如何在laravel 5.2中编写这个关联查询?

时间:2016-06-10 15:01:19

标签: php laravel laravel-5 eloquent 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

我该怎么办?

1 个答案:

答案 0 :(得分:2)

以下应该可以解决问题:

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