Laravel/PHP Articles

Articles from Laravel/PHP sites on the web

Laravel/PHP Articles
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.

Sign Up For Our Newsletter

Each Week, We curate the top Laravel/PHP articles, jobs, events and More!

We'll send you a weekly email with the latest Laravel/PHP articles, jobs and events. Please review our Code of Conduct in the meantime. Talk to you soon!