I\'m developing an application that lists images, and has multiple tags assigned to each image. I\'d like to be able to find the images that are tagged with all
Manual count and having would work, but you can use simple whereHas instead:
// let it be array of tag ids:
$tagsIds;
$images = Image::whereHas('tags', function ($q) use ($tagsIds) {
$q->whereIn('tags.id', $tagsIds);
}, '=', count($tagsIds))->get();
However mind that both mine and @user3158900's solutions will work only, when you have no duplicates on the pivot table. Using Eloquent's attach method on BelongsToMany relation may lead to such situation.