2016-06-26 20 views
1

クエリビルダを使用して、DB :: raw();を使用せずに他のテーブルへのNothingを条件付けする方法を教えてください。Laravel DB Query Builder "whereNotIn" to another table

$query ="select * from project 
      where prj_usr_id= $user->id 
      and now()<prj_expiry 
      and prj_id not in(select bd_prj_id from bid where bd_status=1) 
      and prj_status='open' 
      order by prj_updated_date desc; 
+2

http://stackoverflow.com/questions/16815551/how-to-do-this-in-laravel-subquery-where-in – herrjeh42

+0

の可能重複モデルのコードにそれを助けた –

+0

herrjeh42 @感謝を投稿してください。 –

答えて

0

これで解決しました。

$results = DB::table('project') 
         ->where('prj_usr_id', '=' , 1) 
         ->where('prj_status', '=' , 'open') 
         ->where('prj_expiry', '<' , Carbon::Now()) 
         ->whereNotIn ('prj_id',function ($query) 
         { 
          $query->select(DB::raw(1)) 
            ->from('bid') 
            ->where('bd_status', '=' , '1') 
            ->get(['bd_prj_id']); 
         }) 
         ->orderBy('prj_updated_date', 'desc') 
         ->paginate(5); 
return $results;