我正在尝试向所有结果添加自定义行和列。我遇到错误,
SQLSTATE [42000]:语法错误或访问冲突:1064您的SQL语法有错误;检查与您的MySQL服务器版本相对应的手册以获取正确的语法,以在'。
png
,badge
,3
。5
,rating
,instructors
和i
内部联接use' at line 1 (SQL: select
之间使用] i.
id,
u.
名称,
i.
约,
i.
price_per_hour,
u.
profile_picture,
https://www.
pngitem [C0 ] com / pimgs / m / 575-5754595_why-bali-best-driver-driver-icons-hd-png.
png.
徽章as
3,
5.
评分as
讲师[C0 ] ifrom
usersas
uinner join
uas
idon
i.
user_id=
gender.
iwhere 0 = 1 and
id` desc limit 10 offset 0)
相关代码:
in (male) order by
我建议在向集合中添加其他数据时坚持使用Laravel Collection .
函数。
public function findInstructors($instrutors, $gender, $offet, $fetch) {
return DB::table('instructors as i')
->select(
'i.id',
'u.name',
'i.about',
'i.price_per_hour',
'u.profile_picture',
'https://www.pngitem.com/pimgs/m/575-5754595_why-bali-best-driver-driver-icons-hd-png.png as badge',
'3.5 as rating'
)
->join('users as u', 'u.id', 'i.user_id')
->whereIn('i.id', $instrutors)
->whereIn('gender', $gender)
->offset($offet)
->limit($fetch)
->orderBy('i.id', 'desc')->get();
}
mysql理解
map()
作为tableName.columnNmae作为别名因为在多余的列名中使用了点号:
为避免这种行为您应该使用MySQL Backticks``:
return DB::table('instructors as i')
->select(
'i.id',
'u.name',
'i.about',
'i.price_per_hour',
'u.profile_picture', // Notice* column removed from here
'3.5 as rating'
)
->join('users as u', 'u.id', 'i.user_id')
->whereIn('i.id', $instrutors)
->whereIn('gender', $gender)
->offset($offet)
->limit($fetch)
->orderBy('i.id', 'desc')
->get()
->map(function($row) {
// Here you can add like this. Keep in mind, this will be common for all the rows.
$row['badge'] = 'https://www.pngitem.com/pimgs/m/575-5754595_why-bali-best-driver-driver-icons-hd-png.png';
return $row;
});
更多详细信息,请参见: 'https://www.pngitem.com/pimgs/m/575-5754595_why-bali-best-driver-driver-icons-hd-png.png as badge',