【发布时间】:2016-01-13 06:58:09
【问题描述】:
您好,我有以下 JSON:
[
{
"name": "Donation",
"collection": {
"name": "Donation Company 1",
"collection": {
"id": 1,
"category": "Donation",
"name": "Some Donation",
"price": 10,
"description": "Hahahaha",
"company": "Donation Company 1"
}
}
},
{
"name": "Donation",
"collection": {
"name": "Donation Company 1",
"collection": {
"id": 2,
"category": "Donation",
"name": "Another Donation",
"price": 50,
"description": "LoL",
"company": "Donation Company 1"
}
}
},
{
"name": "Insurance Company 1",
"collection": {
"name": "Hehe",
"collection": {
"id": 3,
"category": "Insurance",
"name": "Lorem Ipsum Solor",
"price": 25,
"description": "Lmao",
"company": "Insurance Company 1"
}
}
},
{
"name": "Insurance Company 2",
"collection": {
"name": "Donation Company 1",
"collection": {
"id": 5,
"category": "Insurance",
"name": "Sample Extra",
"price": 500,
"description": "Lorem ipsum dolor",
"company": "Insurance Company 2"
}
}
}
]
我在尝试将相似的项目组合在一起时遇到问题,因为同一类别中的所有项目都出现在同一类别数组中。 在每个类别中,拥有同一公司的所有项目最终都在同一个公司数组中。
这是我想要完成的一个示例,我使用在线编辑器来构建 JSON: expected-json-output-when-saved
我使用 laravel collect() 辅助函数来构建我的对象数组,我在 foreach 循环中尝试了几种组合,并且已经使用了几个小时,但仍然无法获得返回预期的 JSON。
【问题讨论】:
标签: json laravel laravel-5 eloquent