2016-05-24 16 views
4

2つのクエリで共用体を作成していますが、結果にwhere句を追加したいが、where句は最初のクエリにのみ追加したい。どうすれば修正できますか?Laravel add where句を結合する結果

$notifications = DB::table('notifications') 
     ->select(DB::raw("notifications.uuid ,notifications.brand_id")) 
    $posts = DB::table('posts') 
     ->select(DB::raw("posts.uuid ,posts.brand_id")) 
     ->unionAll ($notifications) 
     ->orderBy('created_at' , 'desc') 
     ->where('brand_ids' , '=' , '2') 
    $result = $posts->get(); 

私はこのライン

  ->where('brand_id' , '=' , '2') 

は全組合に追加することにしたいが、それは唯一のクエリのいずれかに追加されます。

答えて

0

私はより良い方法があるかどうかわからないが、これは私の作品:

$notifications = DB::table('notifications') 
       ->select(DB::raw("notifications.uuid ,notifications.brand_id")); 
$posts = DB::table('posts') 
     ->select(DB::raw("posts.uuid ,posts.brand_id")) 
     ->unionAll($notifications) 
     ->orderBy('created_at' , 'desc'); 
$result = DB::table(DB::raw("({$posts->toSql()}) as posts")) 
       ->mergeBindings($posts) 
       ->where('brand_id', '2') 
       ->get(); 
関連する問題