Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 1 year ago.
0

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();
Last updated 1 year ago.
0

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();
Last updated 1 year ago.
0

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

Last updated 1 year ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Tobiasartz tobiasartz Joined 21 Nov 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.