Laravel Collection GroupBy with Examples

By Hardik Savani April 16, 2024 Category : Laravel

Hi Artisan,

This article will give you example of laravel collection group by example. 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.

I will give you list of examples of groupby colletion in laravel. so you can easily use it with your laravel 5, laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 application. so let's see bellow example that will helps you lot.


List of Examples

1) Example 1: Laravel Collection Group By Simple Example

2) Example 2: Laravel Collection Group By Preserve Key

3) Example 3: Laravel Collection Group By with Multiple Columns

4) Example 4: Laravel Collection Group By with Date

5) Example 5: Laravel Collection Group By with Count

6) Example 6: Laravel Collection Group By with Sum

Example 1: Laravel Collection Group By Simple Example

public function index()

{

$collection = collect([

'first' => ['id'=>1, 'name'=>'Hardik', 'city' => 'Mumbai', 'country' => 'India'],

'second' => ['id'=>2, 'name'=>'Vimal', 'city' => 'New York', 'country' => 'US'],

'third' => ['id'=>3, 'name'=>'Harshad', 'city' => 'Gujarat', 'country' => 'India'],

'fourth' => ['id'=>4, 'name'=>'Harsukh', 'city' => 'New York', 'country' => 'US'],

]);

$grouped = $collection->groupBy('country');

dd($grouped);

}

Output:

Illuminate\Support\Collection Object

(

[items:protected] => Array

(

[India] => Illuminate\Support\Collection Object

(

[items:protected] => Array

(

[0] => Array

(

[id] => 1

[name] => Hardik

[city] => Mumbai

[country] => India

)

[1] => Array

(

[id] => 3

[name] => Harshad

[city] => Gujarat

[country] => India

)

)

)

[US] => Illuminate\Support\Collection Object

(

[items:protected] => Array

(

[0] => Array

(

[id] => 2

[name] => Vimal

[city] => New York

[country] => US

)

[1] => Array

(

[id] => 4

[name] => Harsukh

[city] => New York

[country] => US

)

)

)

)

)

Example 2: Laravel Collection Group By Preserve Key

here, we will use same example as above but we will pass preserve key as true. so you can compare both output and see. there is difference is "key", here will be key name same.

$collection->groupBy('Key_Name', $preserve_key);

public function index()

{

$collection = collect([

'first' => ['id'=>1, 'name'=>'Hardik', 'city' => 'Mumbai', 'country' => 'India'],

'second' => ['id'=>2, 'name'=>'Vimal', 'city' => 'New York', 'country' => 'US'],

'third' => ['id'=>3, 'name'=>'Harshad', 'city' => 'Gujarat', 'country' => 'India'],

'fourth' => ['id'=>4, 'name'=>'Harsukh', 'city' => 'New York', 'country' => 'US'],

]);

$grouped = $collection->groupBy('country', true);

dd($grouped);

}

Output:

Illuminate\Support\Collection Object

(

[items:protected] => Array

(

[India] => Illuminate\Support\Collection Object

(

[items:protected] => Array

(

[first] => Array

(

[id] => 1

[name] => Hardik

[city] => Mumbai

[country] => India

)

[third] => Array

(

[id] => 3

[name] => Harshad

[city] => Gujarat

[country] => India

)

)

)

[US] => Illuminate\Support\Collection Object

(

[items:protected] => Array

(

[second] => Array

(

[id] => 2

[name] => Vimal

[city] => New York

[country] => US

)

[fourth] => Array

(

[id] => 4

[name] => Harsukh

[city] => New York

[country] => US

)

)

)

)

)

Example 3: Laravel Collection Group By with Multiple Columns

public function index()

{

$collection = collect([

['id'=>1, 'name'=>'Hardik', 'city' => 'Mumbai', 'country' => 'India'],

['id'=>2, 'name'=>'Vimal', 'city' => 'New York', 'country' => 'US'],

['id'=>3, 'name'=>'Harshad', 'city' => 'Gujarat', 'country' => 'India'],

['id'=>4, 'name'=>'Harsukh', 'city' => 'New York', 'country' => 'US'],

]);

$grouped = $collection->groupBy(function ($item, $key) {

return $item['country'].$item['city'];

});

dd($grouped);

}

Output:

Illuminate\Support\Collection Object

(

[items:protected] => Array

(

[IndiaMumbai] => Illuminate\Support\Collection Object

(

[items:protected] => Array

(

[0] => Array

(

[id] => 1

[name] => Hardik

[city] => Mumbai

[country] => India

)

)

)

[USNew York] => Illuminate\Support\Collection Object

(

[items:protected] => Array

(

[0] => Array

(

[id] => 2

[name] => Vimal

[city] => New York

[country] => US

)

[1] => Array

(

[id] => 4

[name] => Harsukh

[city] => New York

[country] => US

)

)

)

[IndiaGujarat] => Illuminate\Support\Collection Object

(

[items:protected] => Array

(

[0] => Array

(

[id] => 3

[name] => Harshad

[city] => Gujarat

[country] => India

)

)

)

)

)

Example 4: Laravel Collection Group By with Date

public function index()

{

$collection = collect([

['id'=>1, 'name'=>'Hardik', 'created_at' => '2020-03-10 10:10:00'],

['id'=>2, 'name'=>'Vimal', 'created_at' => '2020-03-10 10:14:00'],

['id'=>3, 'name'=>'Harshad', 'created_at' => '2020-03-11 10:12:00'],

['id'=>4, 'name'=>'Harsukh', 'created_at' => '2020-03-12 10:12:00'],

]);

$grouped = $collection->groupBy(function($item, $key) {

return \Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $item['created_at'])->format('m/d/Y');

});

dd($grouped);

}

Output:

Illuminate\Support\Collection Object

(

[items:protected] => Array

(

[03/10/2020] => Illuminate\Support\Collection Object

(

[items:protected] => Array

(

[0] => Array

(

[id] => 1

[name] => Hardik

[created_at] => 2020-03-10 10:10:00

)

[1] => Array

(

[id] => 2

[name] => Vimal

[created_at] => 2020-03-10 10:14:00

)

)

)

[03/11/2020] => Illuminate\Support\Collection Object

(

[items:protected] => Array

(

[0] => Array

(

[id] => 3

[name] => Harshad

[created_at] => 2020-03-11 10:12:00

)

)

)

[03/12/2020] => Illuminate\Support\Collection Object

(

[items:protected] => Array

(

[0] => Array

(

[id] => 4

[name] => Harsukh

[created_at] => 2020-03-12 10:12:00

)

)

)

)

)

Example 5: Laravel Collection Group By with Count

public function index()

{

$collection = collect([

['id'=>1, 'name'=>'Hardik', 'city' => 'Mumbai', 'country' => 'India'],

['id'=>2, 'name'=>'Vimal', 'city' => 'New York', 'country' => 'US'],

['id'=>3, 'name'=>'Harshad', 'city' => 'Gujarat', 'country' => 'India'],

['id'=>4, 'name'=>'Harsukh', 'city' => 'New York', 'country' => 'US'],

]);

$grouped = $collection->groupBy('country')->map(function ($row) {

return $row->count();

});

dd($grouped);

}

Output:

Illuminate\Support\Collection Object

(

[items:protected] => Array

(

[India] => 2

[US] => 2

)

)

Example 6: Laravel Collection Group By with Sum

public function index()

{

$collection = collect([

['id'=>1, 'name'=>'Hardik', 'city' => 'Mumbai', 'country' => 'India', 'amount' => 2000],

['id'=>2, 'name'=>'Vimal', 'city' => 'New York', 'country' => 'US', 'amount' => 1000],

['id'=>3, 'name'=>'Harshad', 'city' => 'Gujarat', 'country' => 'India', 'amount' => 3000],

['id'=>4, 'name'=>'Harsukh', 'city' => 'New York', 'country' => 'US', 'amount' => 2000],

]);

$grouped = $collection->groupBy('country')->map(function ($row) {

return $row->sum('amount');

});

dd($grouped);

}

Output:

Illuminate\Support\Collection Object

(

[items:protected] => Array

(

[India] => 5000

[US] => 3000

)

)

I hope it can help you...

Shares