Quick tip for those who want to some events, grouping them by some date value – by day, month, year etc.

Let’s take a look at example – we have this database structure of appointments.

And we want to list the upcoming appointments for 7 days ahead, grouped by day. Like this:

Ok, so here’s the code:

A few explanations here:

  1. The main “trick”: you can use Collection to groupBy any custom logic you want – see groupBy(function ($val) { …
  2. Inside of that groupBy we still need to use Carbon::parse(), even if start_time column is within $dates array of the model.
  3. Use now() helper to manipulate some dates

That’s it! You can read more about available Collection grouping/manipulating methods in the official documentation, or purchase my course “Eloquent: Expert Level”, where I dedicated one of the sections to Collections, see below:

Source link
thanks you RSS link
( https://laraveldaily.com/---result-by-day-month-year/)


Please enter your comment!
Please enter your name here