Php 如果在Laravel的第二个表中不匹配,则无法获取记录

Php 如果在Laravel的第二个表中不匹配,则无法获取记录,php,laravel,laravel-5,Php,Laravel,Laravel 5,我使用的是laravel,我想从第一个表中获取记录,即使它不存在于第二个表中,但现在我只在两个表中都存在的情况下获取记录 : 试着这样做: $module=DB::table('r_module') ->leftJoin('r_perm','r_perm.module_id','=','r_module.id') ->where('r_perm.role_id', $id) ->orWhere('r_perm.role_id', 'IS NULL') -

我使用的是laravel,我想从第一个表中获取记录,即使它不存在于第二个表中,但现在我只在两个表中都存在的情况下获取记录 :

试着这样做:

$module=DB::table('r_module')
   ->leftJoin('r_perm','r_perm.module_id','=','r_module.id')
   ->where('r_perm.role_id', $id)
   ->orWhere('r_perm.role_id', 'IS NULL')
   ->get();
dd(DB::table('r_module')
   ->leftJoin('r_perm','r_perm.module_id','=','r_module.id')
   ->where('r_perm.role_id', $id)
   ->orWhere('r_perm.role_id', 'IS NULL')
   ->toSql());
您还可以使用“dd()”函数调试该查询,如下所示:

$module=DB::table('r_module')
   ->leftJoin('r_perm','r_perm.module_id','=','r_module.id')
   ->where('r_perm.role_id', $id)
   ->orWhere('r_perm.role_id', 'IS NULL')
   ->get();
dd(DB::table('r_module')
   ->leftJoin('r_perm','r_perm.module_id','=','r_module.id')
   ->where('r_perm.role_id', $id)
   ->orWhere('r_perm.role_id', 'IS NULL')
   ->toSql());

这将使您能够根据需要生成它。

使用
toSql
并检查您的查询是否正确