添加要选择的自定义列

问题描述 投票:0回答:2

我正在尝试向所有结果添加自定义行和列。我遇到错误,

SQLSTATE [42000]:语法错误或访问冲突:1064您的SQL语法有错误;检查与您的MySQL服务器版本相对应的手册以获取正确的语法,以在'。pngbadge35ratinginstructorsi内部联接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 ] ifromusersasuinner joinuasidoni.user_id=gender.iwhere 0 = 1 andid` desc limit 10 offset 0)

相关代码:

in (male) order by
laravel query-builder
2个回答
0
投票

我建议在向集合中添加其他数据时坚持使用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();
}

0
投票

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',

© www.soinside.com 2019 - 2024. All rights reserved.