I have Products, Attributes and Categories. I want to create custom attributes array without using foreach. I know I can get that done with foreach but I want to do it with Eloquent query.
I want to create array (Eloquent returned Object) like this:
$attributes = [
'attribute1' => [
'attribute1_property1' => 'attribute1_property1_count_in_all_products',
'attribute1_property2' => 'attribute1_property2_count_in_all_products',
'attribute1_property3' => 'attribute1_property3_count_in_all_products',
],
'attribute2' => [
'attribute2_property1' => 'attribute1_property1_count_in_all_products',
'attribute2_property2' => 'attribute1_property2_count_in_all_products',
'attribute2_property3' => 'attribute1_property3_count_in_all_products',
],
...
]
I have models Product
, Attribute
, AttributeProperty
.
Product::whereHas('categories', function ($q) use ($catId) {
$q->whereId($catId);
})->with('attributeProperties.attribute')->get();
With above code I get list of products and it's attribute options but I want that attribute options grouped by parent attribute and counted how many products had that attribute option.
In other words I want to get all attribute_options
grouped by parent attribute
and counted how many products do that attribute_option
have on this category
. Thank you in advance.
toArray()
on the sub collection? – Jeremy HarrisAttributeOption
I'm not seeingattribute_options
in the schema? – Eric TuckerAttributeProperty
– Buglinjo