0
votes

My method is:

public function show(Tag $tag)
{
    $posts = $tag->posts;
    return view('posts.index',compact('posts'));
}

It works fine but i want to get the posts where posts->user_id is the authenticated user.

public function show(Tag $tag)
{
    $posts = $tag->posts()->where('user_id',Auth::user()->id);
    return view('posts.index',compact('posts'));
}

How do i filter on the related posts table?

this is a many to many relationship with a pivot table present

1

1 Answers

1
votes

What you have should work, but don't forget to get() the results after adding your where:

$posts = $tag->posts()->where('user_id',Auth::user()->id)->get();