如何在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'));
}
我该怎么办?
答案 0 :(得分:2)
以下应该可以解决问题:
$articles = $user->articles()->with('category')->get();