如何在查询生成器中重新制作SQL查询

时间:2018-11-01 11:51:11

标签: laravel query-builder

工作代码

$result = DB::select('SELECT 
        galleries.user_id, 
        galleries.image,
        galleries.title 

        FROM galleries

        LEFT JOIN gallery_master ON gallery_master.gallery_id = galleries.id 

        WHERE gallery_master.master_id = 5

        ORDER BY galleries.id DESC');

我正在尝试翻拍,但出现错误

$result = DB::table('galleries')
        ->leftJoin('gallery_master', 'gallery_master.gallery_id', '=', 'galleries.id ')
        ->select('galleries.user_id', 'galleries.image', 'galleries.title')
        ->where('gallery_master.master_id' = '5')
        ->paginate(20);

错误

 "SQLSTATE[42S22]: Column not found: 1054 Unknown column 'galleries.id ' 
in 'on clause' 
(SQL: select count(*) as aggregate from `galleries` 
left join `gallery_master` on `gallery_master`.`gallery_id` = `galleries`.`id `)

我不知道该怎么做

1 个答案:

答案 0 :(得分:1)

请从“ galleries.id”中删除空间

尝试下面的查询

$result = DB::table('galleries')
        ->select('galleries.user_id', 'galleries.image', 'galleries.title')
        ->leftJoin('gallery_master', 'gallery_master.gallery_id', '=', 'galleries.id') // from here
        ->where('gallery_master.master_id' = '5')
        ->paginate(20);