Christoph Rumpel: GroupBy multiple levels in Laravel
In a post to his site Christoph Rumpel shows the Laravel (well, Eloquent) users out there how to use groupBy at multiple levels for customizing and sorting the results of your database queries.
Since Laravel v5.5.29 you can group collections by multiple levels. Let's see what this means and how this works.
The post starts with some of the current functionality, showing how the groupBy
could be used before with a simple collection to group student data by skill level. It then gets into the new functionality, modifying the example to group by both the skill level and by the "teacher" value. The result is a sorted set of results where the top level is the skill level and, inside of each, the teachers with students under that. The post finishes by "going crazy" and showing the grouping by event more criteria, adding "name" and "email" columns to the list.