最后,我使用这段代码来获得期望的结果
$res = DB::select("SELECT posts.* FROM posts JOIN replies ON posts.id = replies.post_id
JOIN comments ON posts.id = comments.post_id OR replies.id = comments.reply_id
WHERE comments.handle = $request->handle AND posts.handle != $request->handle
GROUP BY posts.id");
$res1 = DB::table('posts')
->join('comments',function ($join){
$join->on('posts.id','=','comments.post_id')
->where('comments.handle','=',$request->handle)
->Where('posts.handle','<>',$request->handle);
})
->groupBy('posts.id')
->get(['posts.*']);
$results = array_merge($res,$res1);
$results= collect($results);
$results = $results->unique();