Solution: $prices = ServicePrice::where('price', '>', 0) ->select('category_id', 'client_id', 'service', 'price', 'regio_name') ->where('category_id','=','106') ->get() ->groupBy('category_id') ->mapWithKeys(function($i,$k) { return [$k=>$i->mapWithKeys(function($a,$b) { return [$a->client_id=>$a]; })]; });
Dec 25, 2021
1
Level 2
Eloquent question
Dear All,
I would like tranform tist SQL table to a 2 level Array. How I could be done with a nice one liner, or other solutionwould be interesting also
select client_id, category_id, service, price, regio_name from service_prices
I would like have a Array like this:
Categories['category_id']['client_id']=Array of full SQL row
# client_id category_id service price regio_name
1 2 106 Csík 1987 Baranya
2 37 106 Csík 6372 Somogy
3 43 106 Csík 3958 Szabolcs-Szatmár-Bereg
First level is OK, but the second level with client_id : Is it possible do with one liner or I should go through the elements of the array in a loop
Thanks gergely
$prices = ServicePrice::where('price', '>', 0)
->select('client_id', 'category_id', 'service', 'price', 'regio_name')
->where('category_id','=','106')
->get()
->mapToGroups(function ($price, $k) {
return [$price->category_id =>$price];
});
dump($prices->toArray());
dd($prices[106]->mapWithKeys(function($i,$k) {
return [$i->client_id=>$i];
})->toArray());
array:1 [▼
106 => array:3 [▼
0 => array:5 [▼
"client_id" => "2"
"category_id" => "106"
"service" => "Csík"
"price" => "1987"
"regio_name" => "Baranya"
]
1 => array:5 [▶]
2 => array:5 [▶]
]
]
array:3 [▼
2 => array:5 [▼
"client_id" => "2"
"category_id" => "106"
"service" => "Csík"
"price" => "1987"
"regio_name" => "Baranya"
]
37 => array:5 [▶]
43 => array:5 [▶]
]
Level 2
Please or to participate in this conversation.