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)

Create an Account!

πŸ‘€ Just want to lurk?

That's fine, you can still create an account and turn on features like 🌚 dark mode.