I am new Laravel and I just want to group my array by key. Here's what I have done so far:
Code:
$vehicles = DB::select('call get_vehicles');
return $vehicles;
Return Value:
[
{
"vhc_id":"001",
"vhc_make":"Toyota",
"vhc_model":"HiluxSR"
},
{
"vhc_id":"001",
"vhc_make":"Toyota",
"vhc_model":"HiluxSR5"
},
{
"vhc_id":"001",
"vhc_make":"Toyota",
"vhc_model":"Landcruiser"
},
{
"vhc_id":"002",
"vhc_make":"Ford",
"vhc_model":"Ranger"
},
{
"vhc_id":"002",
"vhc_make":"Ford",
"vhc_model":"Falcon"
}
]
I just want something like this:
[
{
"vhc_id":"001",
"vhc_make":"Toyota",
"vhc_model":[
"HiluxSR",
"HiluxSR5",
"Landcruiser"
]
},
{
"vhc_id":"002",
"vhc_make":"Ranger",
"vhc_model": [
"Ranger",
"Falcon"
]
},
]
I tried foreach to $vehicles variable but it says Cannot use object of type stdClass as array is there any way to achieve this? thanks in advance. Have a good day~