You need a whereHas with a whereIn statement. The code would be something like this
$tagArray = array('comedy', 'thriller', 'horror');
$videos = Video::whereHas('tags', function($tagQuery){
$tagQuery->whereIn('title', $tagArray);
})->get();
If every video must have all the tags at the same time, then use this
$tagArray = array('comedy', 'thriller', 'horror');
$videoQuery = Video::...;
foreach($tagArray as $tag){
$videoQuery =$videoQuery ->whereHas('tags', function($tagQuery){
$tagQuery->where('title', '=', $tag);
});
}
$videos = $videoQuery->get();
Firtzberg said:
If every video must have all the tags at the same time, then use this
$tagArray = array('comedy', 'thriller', 'horror'); $videoQuery = Video::...; foreach($tagArray as $tag){ $videoQuery =$videoQuery ->whereHas('tags', function($tagQuery){ $tagQuery->where('title', '=', $tag); }); } $videos = $videoQuery->get();
This was exactly what i was looking for! Thank you so much :D
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community