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

Latest comments (0)

DEV

Thank you.

Β 
Thanks for visiting DEV, we’ve worked really hard to cultivate this great community and would love to have you join us. If you’d like to create an account, you can sign up here.