У меня проблема с функцией группировки. Пожалуйста, мне нужна помощь. Вот мой код
$rate = Rate::orderBy('created_at', 'desc')->get()->groupBy([function($item) {
return $item->location;
},
function($item) {
return ['currency' => $item->currency];
},
function($item) {
return ['time' => $this->rateTime($item->created_at->format('H'))];
}]
);
Это результат его возврата
"data": {
"Lagos": {
"GBP": {
"Afternoon": [
{
"id": 6,
"currency": "GBP",
"buy_rate": "350",
"sell_rate": "450",
"location": "Lagos",
"created_at": "2020-05-25 16:00:41",
"updated_at": "2020-05-25 16:00:41"
}
]
},
"USD": {
"Morning": [
{
"id": 5,
"currency": "USD",
"buy_rate": "390",
"sell_rate": "450",
"location": "Lagos",
"created_at": "2020-05-25 04:04:00",
"updated_at": "2020-05-25 04:04:00"
}
]
},
"EUR": {
"Evening": [
{
"id": 4,
"currency": "EUR",
"buy_rate": "530",
"sell_rate": "545",
"location": "Lagos",
"created_at": "2020-05-21 18:33:30",
"updated_at": "2020-05-21 18:33:30"
}
]
},
"NGN": {
"Afternoon": [
{
"id": 3,
"currency": "NGN",
"buy_rate": "20.45",
"sell_rate": "32.34",
"location": "Lagos",
"created_at": "2020-05-21 16:41:19",
"updated_at": "2020-05-21 16:41:19"
},
{
"id": 2,
"currency": "NGN",
"buy_rate": "20.45",
"sell_rate": "32.34",
"location": "Lagos",
"created_at": "2020-05-21 16:39:15",
"updated_at": "2020-05-21 16:39:15"
}
]
}
},
"Ghana": {
"NGN": {
"Afternoon": [
{
"id": 1,
"currency": "NGN",
"buy_rate": "20.45",
"sell_rate": "32.34",
"location": "Ghana",
"created_at": "2020-05-21 16:38:26",
"updated_at": "2020-05-21 16:38:26"
}
]
}
}
}
}
Но это то, что мне нужно
"data": {
"Location": "Lagos",
{
"Currency": "GBP" {
"Afternoon": [
{
"id": 6,
"currency": "GBP",
"buy_rate": "350",
"sell_rate": "450",
"location": "Lagos",
"created_at": "2020-05-25 16:00:41",
"updated_at": "2020-05-25 16:00:41"
}
]
},
"Currency": "USD",
{
"Morning": [
{
"id": 5,
"currency": "USD",
"buy_rate": "390",
"sell_rate": "450",
"location": "Lagos",
"created_at": "2020-05-25 04:04:00",
"updated_at": "2020-05-25 04:04:00"
}
]
},
"Currency": "EUR" {
"Evening": [
{
"id": 4,
"currency": "EUR",
"buy_rate": "530",
"sell_rate": "545",
"location": "Lagos",
"created_at": "2020-05-21 18:33:30",
"updated_at": "2020-05-21 18:33:30"
}
]
},
"Currency": "NGN" {
"Afternoon": [
{
"id": 3,
"currency": "NGN",
"buy_rate": "20.45",
"sell_rate": "32.34",
"location": "Lagos",
"created_at": "2020-05-21 16:41:19",
"updated_at": "2020-05-21 16:41:19"
},
{
"id": 2,
"currency": "NGN",
"buy_rate": "20.45",
"sell_rate": "32.34",
"location": "Lagos",
"created_at": "2020-05-21 16:39:15",
"updated_at": "2020-05-21 16:39:15"
}
]
}
},
"Location": "Ghana" {
"Currency": "NGN" {
"Afternoon": [
{
"id": 1,
"currency": "NGN",
"buy_rate": "20.45",
"sell_rate": "32.34",
"location": "Ghana",
"created_at": "2020-05-21 16:38:26",
"updated_at": "2020-05-21 16:38:26"
}
]
}
}
}
}
Я хочу получить объект с ключами и значениями, а не только значениями. Пробовал разные способы, но все еще получаю. Буду рад, если кто-нибудь сможет мне помочь.