Well, i don't see any "orderBy" in your code, maybe something like :
Post::all()->groupBy(function($date) {
return \Carbon\Carbon::parse($date->created_at)->format('d-M-y');
})->orderBy('created_at');
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I want to group posts by each day and order them like a calendar for creating a blog archive.
If I use this
Post::all()->groupBy(function($date) {
return \Carbon\Carbon::parse($date->created_at)->format('d-M-y');
});
I get output something like this, for sure posts are grouped according to dates, but dates itself are not properly ordered like calander, they are random.
Current output Random order of dates

Expeted output Dates are properly ordered from current date to older dates

First of all created_at is already returned as a carbon object. This should work :
$posts = Post::all()->groupBy(function($item){ return $item->created_at->format('d-M-y'); });
Then use uksort to sort the array by keys using a cmp callback :
uksort($posts, function($a, $b)
{
$ta = \Carbon\Carbon::parse($a)->timestamp;
$tb = \Carbon\Carbon::parse($b)->timestamp;
return $ta - $tb; // or $tb - $ta depending on the order you want
})
I don't know Carbon much so maybe Im wrong with its usage but I guess you get the idea.
Or else you can sort them first
$posts = Post::latest()->get()->groupBy(function($item)
{
return $item->created_at->format('d-M-y');
});
I think it is cleaner.
Please or to participate in this conversation.