How to get this result:
foreach ($products as $product) {
$product->name
foreach ($product->attributes as $attribute) {
$attribute->name
foreach ($attribute->values as $value) {
$value->name
}
}
}
And
{
"name": "Product",
"attributes": [
{
"name": "Attribute 1",
"values": [
"name": "Value 1",
"name": "Value 2",
]
}
]
}
Instead of this:
foreach ($products as $product) {
$product->name
foreach ($product->attributeValues as $attributeValue) {
$attributeValue->attribute->name
$attributeValue->name
}
}
And
{
"name": "Product",
"attribute_values": [
{
"attribute_name": "Attribute 1",
"value_name": "Value 1",
},
{
"attribute_name": "Attribute 1",
"value_name": "Value 2",
}
]
}
Database
products
--- id
--- name
attributes
--- id
--- name
attribute_values
--- id
--- attribute_id
--- name
attribute_value_product
--- id
--- attribute_value_id
--- product_id
Products
Attributes
Attribute Values
I'm struggling to find solution but failed so i posted here for get some solution.
Please help me.
with
, select
, groupBy
instead of having 3 foreach