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)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay