Skip to content
Advertisement

Show the posts which have specific tags or categories

I want to show the posts which have specific tags or categories as I mentioned in the title; for example, one of my posts has 3 tags “php”,”laravel”,”regex” I want to get those posts that have laravel tag in their many to many relationships.

Advertisement

Answer

First, make sure you have set the right relations on your models. then use Querying Relationship Existence. So, you can:

$posts = Post::whereHas('tags', function ($tags) use ($tagName) {
        $tags->where('name', $tagName);
    })
    ->whereHas('ORMRoute', functions ($routes) use ($routeName) {
        $routes->where('route', $routeName);
    })
    ->get();
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement