Following the tags example in the Laravel docs, I've successfully implemented image tagging, and now I'm trying to search by said tags.
Pulling an image from a single tag works well, but I'm hitting a roadblock when trying to search for images containing multiple tags.
This is the closest I've gotten, and it works as expected but it is not what I desire.
/**
* $tag string, example: 'baseball'
* $tags array, example: ['baseball','stadium','new.york.yankees']
*/
Image::whereHas('tags', function($query) use ($tag,$tags) {
if (count($tags)) {
// Retrieves images tagged as 'baseball' OR 'stadium' OR 'new.york.yankees'
// Instead, I want it to retrieve images that have all three tags
$query->whereIn('tag', $tags);
// The following returns no results, though there are images tagged correctly.
// I presume this is an incorrect approach.
foreach ($tags as $tag) {
$query->where('tag', '=', $tag);
}
} else {
// Retrieves images tagged as 'baseball'
$query->where('tag', '=', $tag);
}
})->orderBy('created_at', 'desc')
->paginate(config('app.images_per_page'))
I'm stumped, overtired, and getting nowhere while searching for similar examples. What am I missing? What is the correct terminology for what I am trying to achieve, so I can add it to my vocabulary for future endeavors?
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire