Eloquent: Grouping and Sorting
Grouping by related models
To group results by related models, you can use the groupBy
method. This method allows you to
group results by a specific column or relationship. In this example, we group courses by their
associated modules:
$course = Course::select('id', 'title', 'slug')
->with([
'modules:id,course_id,title',
'modules.lessons:id,module_id,title'
])->find($courseId);