2017-09-05 3 views
0

tables to join MySQL의 고급 내가 세 개의 테이블이 5.2

laravel에 조인 게시물, 댓글 및

  • 게시물 테이블과 동일한 테이블 답글하지만 게시물의 응답
  • 게시물 테이블이 응답 덧글
  • 답글 테이블도 댓글이 있습니다
  • 댓글 테이블은 댓글 테이블 및 게시물 테이블
  • 의 의견을 저장하는 것입니다 그때 또한 해당 게시물이 포함 된 게시물의 응답이 코멘트가 있고, 결과 게시물 고유해야합니다 비록 의견이 모든 게시물을 얻을하고자은
  • ...

이 내가 시도 무엇 ::

$RC = DB::table('comments') 
    ->join('replies','comments.reply_id','=','replies.id') 
    ->where('comments.handle',$request->handle) 
    ->where('replies.handle','!=',$request->handle) 
    ->groupBy('replies.post_id') 
    ->get(['replies.post_id']); 

$PC = DB::table('comments') 
    ->join('posts', 'comments.post_id', '=', 'posts.id') 
    ->where('comments.handle',$request->handle) 
    ->where('posts.handle','!=',$request->handle) 
    ->groupBy('comments.post_id') 
    ->get(['comments.post_id']); 
+0

을 원하는 얻을이 코드를 사용하고 있습니다? –

+0

공식 문서를 읽으십시오 : https://laravel.com/docs/5.2/eloquent-relationships를 읽고 그것에 관해서 한 일을 – SUB0DH

+0

@ShaunakShukla 제가 게시 한 것을 게시했습니다 – BCPNAYAK

답변

0

마지막으로 나는 결과

이 구현 한 어떤
$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();