Multiple groupBy on Laravel Collection

I am trying to group a collection by two clauses in Laravel 5, but it only groups it by the first one. Can someone please help me out? Here's my code.

$query = Activity::all()->groupBy('performed_at_year_month', 'performer_id')->toJson();

The solution in https://stackoverflow.com/a/30469061/221745 can be improved on I think. The key is to remember that a grouped by collection is a collection of collections. Therefore, the following will work:

<?php

$recs = new \Illuminate\Database\Eloquent\Collection($query);
$grouped = $recs->groupBy('performed_at_year_month')->transform(function($item, $k) {
    return $item->groupBy('performer_id');
});

Where $grouped contains your final result

GroupBy multiple levels in Laravel Since Laravel v5.5.29 you can group collections by multiple levels. Let's see what this means and how this works.


You can try chaining them, however I am not sure it will get you the results you want. May want to check to be sure

$query = Activity::all()->groupBy('performed_at_year_month')->groupBy('performer_id')->toJson();

I just checked this and you are correct, it's not preserving keys, although I think I can guess why. If a key happens to be the same, things will get overwritten. It could also be difficult to determine which key is what and overall feels a little "hacky" where there is likely a much better solution.

However, I think I've figured out how to do this.

$query = Activity::all();
$activities = new \Illuminate\Database\Eloquent\Collection($query);
$activities = $activities->groupBy('performed_at_year_month')->toArray() + $activities->groupBy('performer_id')->toArray();
echo json_encode($activities);

Today, We want to share with you Group By using Laravel Collection GroupBy Example.In this post we will show you laravel collection contains multiple, hear for laravel collection to array of objects we will give you demo and example for implement.In this post, we will learn about laravel collection group by and sum with an example.


Instead of passing a string key, you may pass a callback. The callback should return the value you wish to key the group by:

$collection->groupBy(function($item, $key){
            return $item["key-1"]."-".$item["key-2"];
        });

Reference:- https://laravel.com/docs/5.7/collections#method-groupby

The groupBy method is used to group a collection based on a given value, represented by the $groupBy parameter. An argument passed to $groupBy can be a simple string, representing the value that the collection should be grouped by, or a callback that returns the value the collection should be grouped by. By default, the groupBy method does not preserve the keys of the collection when grouping, but that can be changed by passing true as the argument to the $preserveKeys parameter.


Here you will learn laravel collection groupby two columns. you will learn laravel collection group by with sum. you can see laravel collection group by with count. I will give you very simple example of laravel collection with two columns, map, sum, count, date, preserve key etc.


The groupBy method is used to group a collection based on a given value, represented by the $groupBy parameter. An argument passed to $groupBy can be a simple string, representing the value that the collection should be grouped by, or a callback that returns the value the collection should be grouped by.


Forum Laravel [Collection] Nested groupBy. Reply Follow All Threads Popular This Week Popular All Time Solved Unsolved No Replies Yet Leaderboard kubaszymanowski.