How to serialize properties of an object in a multidimensional structure?

I use Laravel framework, appropriately when I do a dB query, as a result get such a structure:
Collection {#288 ▼
 #items: array:7 [▼
 0 => {#323 ▼
 +"record_id": 1
 +"city_id": 4
 +"value": "90"
 +"from_date": "2017-11-01 00:00:00"
 +"to_date": "2018-11-30 23:59:59"
}
 1 => {#321 ▼
 +"record_id": 2
 +"city_id": 4
 +"value": "70"
 +"from_date": "2018-12-01 00:00:00"
 +"to_date": "2019-03-31 23:59:59"
}
 2 => {#324 ▶}
 3 => {#326 ▶}
 4 => {#327 ▶}
 5 => {#328 ▶}
 6 => {#329 ▶}
]
}

As it Eloqunet or native php result
mind
Collection {#288 
 #items: array:3 [
 + city_id(4) => {
 + value(90) => {
 +"from_date": "2017-11-01 00:00:00"
 +"to_date": "2018-11-30 23:59:59"
}
 + value(70) => {
 +"from_date": "2018-12-01 00:00:00"
 +"to_date": "2019-11-30 23:59:59"
}
}
 + city_id(3) => {
//etc
}
}


Documentation Eloquenet there are many methods that allow you to get a similar result, but perhaps because of the lack of full understanding of how to achieve results, I can't. At the moment the best that I have
left:
Collection {#345 ▼
 #items: array:3 [▼
 4 => Collection {#338 ▼
 #items: array:3 [▼
 90 => Collection {#335 ▼
 #items: array:1 [▼
 0 => {#323 ▼
 +"value": "90"
 +"from_date": "2017-11-01 00:00:00"
 +"to_date": "2018-11-30 23:59:59"
}
]
}
 70 => Collection {#336 ▶}
 60 => Collection {#337 ▶}
]
}
 3 => Collection {#341 ▼
 #items: array:2 [▼
 90 => Collection {#339 ▼
 #items: array:1 [▼
 0 => {#326 ▼
 +"value": "90"
 +"from_date": "2017-11-01 00:00:00"
 +"to_date": "2019-02-28 23:59:59"
}
]
}
 60 => Collection {#340 ▶}
]
}
 10 => Collection {#344 ▶}
]
}

Almost, but because the intermediate array at the end of the call, the desired properties will look similar city_id->value[0]->property, which is not very good, because then the data will need to be Packed into json and then runs on the client side. How to do it properly?
March 19th 20 at 08:40
1 answer
March 19th 20 at 08:42
->groupBy('city_id')

edit: ->groupBy('city_id')->map->groupBy('value')
The result that I showed above, obtained via ->groupBy(['city_id','value'])
I read the documentation and tried many things before writing here. - gerson.Jerde78 commented on March 19th 20 at 08:45
@gerson.Jerde78, and did not finish. Then ->groupBy('city_id')->map->groupBy('value') - johnpaul.Hartmann commented on March 19th 20 at 08:48
@johnpaul.Hartmann, same result.
That's right now yet, solve the same problem:
There is such
structure:
Collection {#370 ▼
 #items: array:17 [▼
 0 => array:1 [▼
 2017 => array:1 [▼
 "November" => array:2 [▼
 "start" => "2017-11-01 00:00:00"
 "end" => "2017-11-30 23:59:59"
]
]
]
 1 => array:1 [▼
 2017 => array:1 [▼
 "December" => array:2 [▼
 "start" => "2017-12-01 00:00:00"
 "end" => "2017-12-31 23:59:59"
]
]
]
 2 => array:1 [▼
 2018 => array:1 [▼
 "January" => array:2 [▶]
]
]
 3 => array:1 [▼
 2018 => array:1 [▼
 "February" => array:2 [▶]
]
]
 4 => array:1 [▶]
 5 => array:1 [▶]
 6 => array:1 [▶]
 7 => array:1 [▶]
 8 => array:1 [▶]
 9 => array:1 [▶]
 10 => array:1 [▶]
 11 => array:1 [▶]
 12 => array:1 [▶]<code lang="php">

</code>
 13 => array:1 [▶]
 14 => array:1 [▶]
 15 => array:1 [▶]
 16 => array:1 [▶]
]
}


I tried to simplify it so that year was repeated once.
Used:
->mapToGroups(function($item,$key) {;
 return $item[key($item)] = $item;
});

Received:
Collection {#375 ▼
 #items: array:3 [▼
 2017 => Collection {#372 ▼
 #items: array:2 [▼
 0 => array:1 [▼
 "November" => array:2 [▼
 "start" => "2017-11-01 00:00:00"
 "end" => "2017-11-30 23:59:59"
]
]
 1 => array:1 [▼
 "December" => array:2 [▼
 "start" => "2017-12-01 00:00:00"
 "end" => "2017-12-31 23:59:59"
]
]
]
}
 2018 => Collection {#373 ▶}
 2019 => Collection {#374 ▶}
]
}

That's where it came from? In principle, I understand that it's a new instance of the collection, but as it forms I do not understand. The code inside the $item array. - gerson.Jerde78 commented on March 19th 20 at 08:51
@gerson.Jerde78, in a sense, identical? It should give you what you want)

About the second example - again, isn't that what you wanted? - johnpaul.Hartmann commented on March 19th 20 at 08:54
@johnpaul.Hartmann, No, I wanted the wrapping of arrays was not, ie, in the latter case, it would mean that the numeric keys(which are 0) are text ie
so:
Collection {#375 ▼
 #items: array:3 [▼
 2017 => Collection {#372 ▼
 #items: array:2 [▼
 "November" => array:2 [▼
 "start" => "2017-11-01 00:00:00"
 "end" => "2017-11-30 23:59:59"
]
 "December" => array:2 [▼
 "start" => "2017-12-01 00:00:00"
 "end" => "2017-12-31 23:59:59"
]
]
}
 2018 => Collection {#373 ▶}
 2019 => Collection {#374 ▶}
]
}

- gerson.Jerde78 commented on March 19th 20 at 08:57

Find more questions by tags Laravel