Quick tips on grouping a list of some events by day/month/year.

As an example, there is a database structure of appointments

Now, we want to list the upcoming appointments for 7 days ahead that grouped by days. The output like image below:



Code on how to do it:
Some explanations:
  • The main “trick”: You can use collection result to groupBy any custom logic you want :- see groupBy(function ($val) { ….. });
  • Inside of that groupBy, we still need to use Carbon::parse(), even if start_time column is within $dates array of the model.
  • Use now() helper to manipulate some dates.

Credit to: https://bit.ly/3cDvh7P