我正在对两个查询进行联合,我想在结果中添加一个where子句,但是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 :(得分: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();