Answer the question
In order to leave comments, you need to log in
How to filter Products by all specified Tags for each of them?
There is a Product and Tags model. I linked them via belongsToMany.
How to filter Products by all specified Tags for each of them? i.e. for example, so that each product has tags 44,34,21,54 (not at least one of them, but all)
$tags_pages=[44,34,21,54]; // Это лишь пример массива. Элементы и количество массива меняется.
$Product = Product::whereHas('tags', function($query) use($tags_pages)
{
$query->whereIn('id',$tags_pages);
})->get();
$tags_pages=[44,34,21,54]; // Это лишь пример массива. Элементы и количество массива меняется.
$Product = Product::whereHas('tags', function($query) use($tags_pages)
{
foreach($tags_pages as $tag){
$query->whereIn('id', $tags_pages);
}
})->get();
Answer the question
In order to leave comments, you need to log in
decision:
$tags_pages=[44,34,21,54]; // Это лишь пример массива. Элементы и количество массива меняется.
$Product = Product::whereHas('tags', function($query) use($tags_pages)
{
$query->whereIn('id',$tags_pages);
}, '=', count($tags_pages))->get();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question