Sorry for the boring question, but I'm having trouble building this query in Eloquent.
I have a product with several skus. Each sku has many attributes.
Each attribute has a type which could be something like "size" or "color", and a value, which would be something like "M", or "black".
Each product has an arbitrary number of attributes, which I've made selectable in a form.
Now, I have a list of each attribute and its value.
[
'size' => 'M',
'color' => 'black',
]
Great. Now I have to fetch the SKU(s) which correspond to these particular attributes.
For example, this works when I have just ONE attribute.
$product->skus->whereHas('attributes', function($query) {
return $query->where(['type' => 'size', 'value' => 'M']);
})->get();
But how do I build a query that returns only the skus that have attributes which match ALL of the qualifications?
And remember, these attributes are arbitrary in number. I can't hardcode it; I just have to pass the array of type-value pairs.
I figured out how to do this manually – but again it doesn't allow for the arbitrary number of attributes. Plus, it's clunky. Something like this:
$product->skus->whereHas('attributes', function($query) {
return $query->where(['type' => 'size', 'value' => 'M']);
})->whereHas('attributes', function($query) {
return $query->where(['type' => 'color', 'value' => 'white']);
})->get();
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire