2014-12-03 67 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(); 

當我打印$data得到的結果,

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

這不是預期的答案。我期待答案如下,

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(); 

這應該給你相應的結果