DEV Community

Cover image for Laravel 9 - Use groupBy on Collections
Sandro Jhuliano Cagara
Sandro Jhuliano Cagara

Posted on

Laravel 9 - Use groupBy on Collections

If you want to group result by some condition which isn’t a direct column in your database, you can do that by providing a closure function.

$users = User::all()->groupBy(function($item) {
    return $item->created_at->format('Y-M-D');
});
Enter fullscreen mode Exit fullscreen mode

Top comments (0)