Sports games by date, registered users by date etc.
Here’s the controller code:use App\User; class UserController extends Controller { public function index() { $users = User::orderBy('created_at')->get()->groupBy(function($item) { return $item->created_at->format('Y-m-d'); }); return view('users', compact('users')); } }A few notices here:Function groupBy() is called AFTER the get() , it is a Collection function and not Eloquent.
If you do groupBy() before get() , it wouldn’t work, cause Eloquent groupBy() doesn’t accept callback parameter.
If you do before , it wouldn’t work, cause Eloquent groupBy() doesn’t accept callback parameter.
In short, you can do groupBy() with callback function, after getting all query results.
Newsletter

Get the latest Laravel/PHP jobs, events and curated articles straight to your inbox, once a week

Glimpse streamlines Laravel development by seamlessly deploying GitHub pull requests to preview environments with the help of Laravel Forge. Glimpse streamlines Laravel development by seamlessly deploying GitHub pull requests to preview environments with the help of Laravel Forge.
Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Shirts painstakingly handcrafted by under-caffeinated developers. Shirts painstakingly handcrafted by under-caffeinated developers.
Community Partners