Laravel Collection GroupBy with Examples

Laravel Collection GroupBy with Examples

In this post we will give you information about Laravel Collection GroupBy with Examples. Hear we will give you detail about Laravel Collection GroupBy with ExamplesAnd how to use it also give you demo for it if it is necessary.

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 and laravel 7 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:

IlluminateSupportCollection Object

(

[items:protected] => Array

(

[India] => IlluminateSupportCollection 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] => IlluminateSupportCollection 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:

IlluminateSupportCollection Object

(

[items:protected] => Array

(

[India] => IlluminateSupportCollection 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] => IlluminateSupportCollection 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:

IlluminateSupportCollection Object

(

[items:protected] => Array

(

[IndiaMumbai] => IlluminateSupportCollection Object

(

[items:protected] => Array

(

[0] => Array

(

[id] => 1

[name] => Hardik

[city] => Mumbai

[country] => India

)

)

)

[USNew York] => IlluminateSupportCollection 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] => IlluminateSupportCollection 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 CarbonCarbon::createFromFormat('Y-m-d H:i:s', $item['created_at'])->format('m/d/Y');

});

dd($grouped);

}

Output:

IlluminateSupportCollection Object

(

[items:protected] => Array

(

[03/10/2020] => IlluminateSupportCollection 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] => IlluminateSupportCollection Object

(

[items:protected] => Array

(

[0] => Array

(

[id] => 3

[name] => Harshad

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

)

)

)

[03/12/2020] => IlluminateSupportCollection 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:

IlluminateSupportCollection 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:

IlluminateSupportCollection Object

(

[items:protected] => Array

(

[India] => 5000

[US] => 3000

)

)

I hope it can help you…

Hope this code and post will helped you for implement Laravel Collection GroupBy with Examples. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github