Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Lina's avatar
Level 1

Eloquent relationships question (whereHas)

Hi friends,

how can I pass a param to the where inside the function?

public function getPostTags($tag_id = null)
{
    return \App\Post::whereHas('post_tags', function ($query){
        $query->where('post_tags.tag_id', '=', $tag_id);
    })->get();
}

I have the table "Post" with a list of posts and the table"Post_tags" which relation the "posts" with "tags" (post_id, tag_id). Then I want to get the posts with a concret tag_id.

0 likes
1 reply

Please or to participate in this conversation.