I want to filter product using their attributes using below query
return Product::with('attributes')
->whereHas('attributes', function ($query) use ($attribute_id_array,$attribute_value_array){
for($k=0;$k<count($attribute_id_array);$k++)
{
$attr_id=$attribute_id_array[$k];
$attr_val=$attribute_value_array[$k];
$query->where(function($q) use ($attr_id, $attr_val) {
$q->where('attribute_id', $attr_id)->where('value', $attr_val);
});
}
})->paginate(10);
I want to check if attributes of a product exist then filter it using the loop to filter out products that are in attributes_value & attributes_id array which does not match but presently this query is not filtering through attributes.
Here are the structure of tables and relations
Product Table :
|id | Title | Price|
-------------------------
|1 | Title 1 | 5000|
|2 | Product 2 | 7000|
and the other is
product_attribute table:
|id | product_id | attribute_id | attribute_name | value|
---------------------------------------------------------
|1 | 1 | 5 | Color | Red |
|2 | 1 | 6 | Size | XL |
|3 | 2 | 5 | Color | Green|
Product and Product attribute is related with following relation (In the product model):
public function attributes()
{
return $this->hasMany(ProductsAttribute::class, 'product_id ');
}
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire