DEV Community

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

Posted on

4 3

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)

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

👥 Ideal for solo developers, teams, and cross-company projects

Learn more

Retry later