November 7th, 2022
accepts column name or function
1// group by user_id, which is column name on posts table2$posts = Post::with('author')->get()->groupBy('user_id');3 4// group by also accepts function: group by author name (author is relationship on posts model)5$posts = Post::with('author')->get()->groupBy(fn($post) => $post->author->name);
accepts array of 2 column names or 2 functions:
1// group by author name and published year2$posts = Post::with('author')->get()3 ->groupBy([fn($post) => $post->author->name, fn($post) => $post->published_at->year]);4 5dd($posts)