将原始复杂SQL查询转换为Eloquent

时间:2016-05-07 16:13:37

标签: php laravel laravel-5 eloquent

任何人都可以帮我翻译成Eloquent吗?

select * from resources 
left join links 
    on links.resource_id = resources.id 
        and (links.ud_id IS NULL OR links.ud_id = '7') 
where resources.user_id = '1' 
    and resources.subject_id = '4'

提前谢谢

1 个答案:

答案 0 :(得分:1)

这应该可以解决问题:

DB::table('resources')
  ->leftJoin('links', function($join) {
    $join->on('links.resource_id', '=', 'resources.id');
    $join->where(function($query) {
      $query->whereNull('links.ud_id');
      $query->orWhere('links.ud_id', '=', 7);
    });
  })
  ->where('user_id', 1)
  ->where('subject_id', 4)
  ->get();
相关问题