DEV Community

Phạm Đức Huy
Phạm Đức Huy

Posted on

Answer: Laravel grouped collection returns object instead of array

If you want array then use this

$outings = Outing::all()->groupBy(function ($item) {
   return Carbon::parse($item['start'])->format('m/d/Y');
})->map(function($item){
    return $item->all();
});

return response()->json($outings, 200);

If you want date as key then

$outings = Outing::all()->groupBy(function ($item) {
   return Carbon::parse($item['start'])->format('m/d/Y');
});

return response()->json($outings->toArray(), 200);

Top comments (0)