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();