2014-12-03 5 views
0

laravel 4.2를 사용하고 있습니다. 나는이 Left Join 쿼리를 실행하면 가입 쿼리가 laravel 4.2에서 제대로 작동하지 않습니다.

$data = DB::table('posts') 
     -> leftJoin('users','posts.user_id','=','users.id') 
     -> where('posts.id',$id) 
     -> select('posts.id as post_id,posts.title as title,users.firstname as fname') 
     -> first(); 

나는

stdClass Object(
    [post_id,posts.title] => 5 
) 

이 예상되는 대답하지의 $data는 결과를 얻었다 인쇄 할 때. 대답은 다음과 같습니다.

stdClass Object(
    [post_id] => 2 
    [title] => Test3 
    [fname] => myname 
) 

올바른 결과를 얻으려면 어떻게해야합니까? 이 쿼리에 문제가 있습니까?

답변

2

변경을 제공해야 선택

$data = DB::table('posts') 
      -> leftJoin('users','posts.user_id','=','users.id') 
      -> where('posts.id',$id) 
      -> select('posts.id as post_id','posts.title as title','users.firstname as fname') 
      -> first(); 
0
$data = DB::table('posts') 
    -> select('posts.id,users.id,posts.title users.firstname') 
    -> leftJoin('users','posts.user_id','=','users.id') 
    -> where('posts.id',$id) 
    -> first(); 

이 적절한 결과를 당신에게

관련 문제