所以有以下表格:
产品
- name
- description
.etc
规格
name
多对多关系的产品规格透视表
product_id
specification_id
value
产品型号
public function specs()
{
return $this->belongsToMany(Specification::class)->withPivot('value');
}
规格型号
public function parent()
{
return $this->belongsTo(Specification::class);
}
public function children()
{
return $this->hasMany(Specification::class, 'parent_id', 'id');
}
所以产品附带的规格有家长和我需要得到他们的家长。有了这些资源,我可以:
{
"data": [
{
"id": 1,
"name": "Apple iPhone 11 Pro 64GB Space Gray",
"slug": "apple-iphone-11-pro-64gb-space-gray",
"price": "99,999",
"description": "description",
"specs": [
{
"name": "Device type:",
"value": "Smartphone"
},
{
"name": "2G standarts:",
"value": "GSM 1800 / GSM 1900 / GSM 850 / GSM 900"
},
]
}
]
}
但我希望这样:
{
"data": [
{
"id": 1,
"name": "Apple iPhone 11 Pro 64GB Space Gray",
"slug": "apple-iphone-11-pro-64gb-space-gray",
"price": "99,999",
"description": "description",
"specs": [
{
"name": "Characteristics",
"children": [
{
"id": 2,
"name": "Device type:",
"value": "Smartphone"
},
{
"id": 2,
"name": "Device type:",
"value": "Smartphone"
},
]
},
{
"name": "Processor",
"children": [
{
"id": 2,
"name": "Processor type:",
"value": "Apple A13 Bionic"
},
{
"id": 2,
"name": "other spec:",
"value": "other spec child value"
},
]
}
]
}
]
}
暂无答案!
目前还没有任何答案,快来回答吧!