2016-06-10 133 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();