2016-06-10 131 views
0

如何在laravel 5.2中編寫這個關聯查詢?如何在laravel 5.2中編寫這個關聯查詢?

關係:

users and articles , one to many. 
categories and articles , one to many. 

查詢:
1,我想獲得當前用戶的文章。
2,我想獲得文章的類別。

public function index() 
{ 
    $user=\Auth::user(); 

    //I use this sentence to query: 
    $articles = $user->articles->with('category'); 

    return view('index', compact('articles')); 
} 

錯誤: enter image description here

我該怎麼辦?

+0

請複製並粘貼所有堆棧跟蹤這裏。現在,您正在使用圖像來顯示其第一行。 –

回答

2

下應該做的伎倆:

$articles = $user->articles()->with('category')->get();