使用 Laravel 和 eloquent 过滤相关的 table
Filter on related table with Laravel and eloquent
我的方法是:
public function show(Tag $tag)
{
$posts = $tag->posts;
return view('posts.index',compact('posts'));
}
它工作正常,但我想获得 posts->user_id 是经过身份验证的用户的帖子。
public function show(Tag $tag)
{
$posts = $tag->posts()->where('user_id',Auth::user()->id);
return view('posts.index',compact('posts'));
}
我如何过滤相关帖子 table?
这是一个多对多的关系,有一个主元table存在
您拥有的应该有效,但不要忘记 get()
添加您的位置后的结果:
$posts = $tag->posts()->where('user_id',Auth::user()->id)->get();
我的方法是:
public function show(Tag $tag)
{
$posts = $tag->posts;
return view('posts.index',compact('posts'));
}
它工作正常,但我想获得 posts->user_id 是经过身份验证的用户的帖子。
public function show(Tag $tag)
{
$posts = $tag->posts()->where('user_id',Auth::user()->id);
return view('posts.index',compact('posts'));
}
我如何过滤相关帖子 table?
这是一个多对多的关系,有一个主元table存在
您拥有的应该有效,但不要忘记 get()
添加您的位置后的结果:
$posts = $tag->posts()->where('user_id',Auth::user()->id)->get();