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

Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future. Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future.
Community Partners