Laravel Group by Example - Yudhy Network

Share on:

Yudhy Network - Laravel group by example tutorial. Here, you will learn how to use laravel groupBy with raw, date, sum, month and laravel collection group by count, etc.

Laravel group by example. In this tutorial you will learn how to use group by with different purpose in laravel.

As well as you learn how to use laravel group with raw, laravel group by multiple, laravel group by date, laravel group by sum, laravel group by month and laravel collection group by count.

Sometime, you may confuse when use group by with laravel eloquent queries. So this tutorial will guide you how to use laravel group by with different purpose with example.

Laravel Group By Examples

This tutorial will demonstrate to you the following laravel group by eloquent method examples:

  • 1: Laravel GroupBy with Collection Example
  • 2: Laravel Collection Group By Preserve Key
  • 3: Laravel Collection Group By with Multiple Columns
  • 4: Laravel Collection Group By with Date
  • 5: Laravel Collection Group By with Count
  • 6: Laravel Collection Group By with Sum

1: Laravel GroupBy with Collection Example

public function index()

{

    $collection = collect([

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

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

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

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

        ]);

  

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

      

    dd($grouped);

}

Result:

Illuminate\Support\Collection Object

(

    [items:protected] => Array

        (

            [India] => Illuminate\Support\Collection Object

                (

                    [items:protected] => Array

                        (

                            [0] => Array

                                (

                                    [id] => 1

                                    [name] => Rahul

                                    [city] => Mumbai

                                    [country] => India

                                )

                            [1] => Array

                                (

                                    [id] => 3

                                    [name] => Sumit

                                    [city] => Gujarat

                                    [country] => India

                                )

                        )

                )

            [US] => Illuminate\Support\Collection Object

                (

                    [items:protected] => Array

                        (

                            [0] => Array

                                (

                                    [id] => 2

                                    [name] => Ronak

                                    [city] => New York

                                    [country] => US

                                )

                            [1] => Array

                                (

                                    [id] => 4

                                    [name] => Harish

                                    [city] => New York

                                    [country] => US

                                )

                        )

                )

        )

)
Recommended:- Laravel Order by 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'=>'Rahul', 'city' => 'Mumbai', 'country' => 'India'],

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

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

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

        ]);

  

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

      

    dd($grouped);

}

Result:

Illuminate\Support\Collection Object

(

    [items:protected] => Array

        (

            [India] => Illuminate\Support\Collection Object

                (

                    [items:protected] => Array

                        (

                            [first] => Array

                                (

                                    [id] => 1

                                    [name] => Rahul

                                    [city] => Mumbai

                                    [country] => India

                                )

                            [third] => Array

                                (

                                    [id] => 3

                                    [name] => Ronak

                                    [city] => Gujarat

                                    [country] => India

                                )

                        )

                )

            [US] => Illuminate\Support\Collection Object

                (

                    [items:protected] => Array

                        (

                            [second] => Array

                                (

                                    [id] => 2

                                    [name] => Sumit

                                    [city] => New York

                                    [country] => US

                                )

                            [fourth] => Array

                                (

                                    [id] => 4

                                    [name] => Harish

                                    [city] => New York

                                    [country] => US

                                )

                        )

                )

        )

)
Recommended:- Laravel Multiple Where Conditions Example

3: Laravel Collection Group By with Multiple Columns

public function index()

{

    $collection = collect([

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

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

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

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

        ]);

  

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

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

                });

      

    dd($grouped);

}

Result:

Illuminate\Support\Collection Object

(

    [items:protected] => Array

        (

            [IndiaMumbai] => Illuminate\Support\Collection Object

                (

                    [items:protected] => Array

                        (

                            [0] => Array

                                (

                                    [id] => 1

                                    [name] => Rahul

                                    [city] => Mumbai

                                    [country] => India

                                )

                        )

                )

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

                (

                    [items:protected] => Array

                        (

                            [0] => Array

                                (

                                    [id] => 2

                                    [name] => Sumit

                                    [city] => New York

                                    [country] => US

                                )

                            [1] => Array

                                (

                                    [id] => 4

                                    [name] => Harish

                                    [city] => New York

                                    [country] => US

                                )

                        )

                )

            [IndiaGujarat] => Illuminate\Support\Collection Object

                (

                    [items:protected] => Array

                        (

                            [0] => Array

                                (

                                    [id] => 3

                                    [name] => Ronak

                                    [city] => Gujarat

                                    [country] => India

                                )

                        )

                )

        )

)
Recommended:- Laravel where Between Eloquent Query

4: Laravel Collection Group By with Date

public function index()

{

    $collection = collect([

            ['id'=>1, 'name'=>'Rahul', 'created_at' => '2020-05-15 15:15:00'],

            ['id'=>2, 'name'=>'Sumit', 'created_at' => '2020-05-15 15:16:00'],

            ['id'=>3, 'name'=>'Ronak', 'created_at' => '2020-05-18 15:18:00'],

            ['id'=>4, 'name'=>'Harish', 'created_at' => '2020-05-19 15:10: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);

}

Result:

Illuminate\Support\Collection Object

(

    [items:protected] => Array

        (

            [05/15/2020] => Illuminate\Support\Collection Object

                (

                    [items:protected] => Array

                        (

                            [0] => Array

                                (

                                    [id] => 1

                                    [name] => Rahul

                                    [created_at] => 2020-05-15 15:15:00

                                )

                            [1] => Array

                                (

                                    [id] => 2

                                    [name] => Sumit

                                    [created_at] => 2020-05-15 15:16:00

                                )

                        )

                )

            [05/18/2020] => Illuminate\Support\Collection Object

                (

                    [items:protected] => Array

                        (

                            [0] => Array

                                (

                                    [id] => 3

                                    [name] => Ronak

                                    [created_at] => 2020-05-18 15:18:00

                                )

                        )

                )

            [05/19/2020] => Illuminate\Support\Collection Object

                (

                    [items:protected] => Array

                        (

                            [0] => Array

                                (

                                    [id] => 4

                                    [name] => Harish

                                    [created_at] => 2020-05-19 15:10:00

                                )

                        )

                )

        )

)
Recommended:- Laravel where Not In Eloquent Query Example

5: Laravel Collection Group By with Count

public function index()

{

    $collection = collect([

            ['id'=>1, 'name'=>'Rahul', 'created_at' => '2020-05-15 15:15:00'],

            ['id'=>2, 'name'=>'Sumit', 'created_at' => '2020-05-15 15:16:00'],

            ['id'=>3, 'name'=>'Ronak', 'created_at' => '2020-05-18 15:18:00'],

            ['id'=>4, 'name'=>'Harish', 'created_at' => '2020-05-19 15:10:00'],

        ]);

  

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

                    return $row->count();

                });

    

    dd($grouped);

}

Result:

Illuminate\Support\Collection Object

(

    [items:protected] => Array

        (

            [India] => 2

            [US] => 2

        )

)
Recommended:- Laravel where Not Between Query Example

6: Laravel Collection Group By with Sum

public function index()

{

    $collection = collect([

            ['id'=>1, 'name'=>'Rahul', 'created_at' => '2020-05-15 15:15:00'],

            ['id'=>2, 'name'=>'Sumit', 'created_at' => '2020-05-15 15:16:00'],

            ['id'=>3, 'name'=>'Ronak', 'created_at' => '2020-05-18 15:18:00'],

            ['id'=>4, 'name'=>'Harish', 'created_at' => '2020-05-19 15:10:00'],

        ]);

  

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

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

                });

    

    dd($grouped);

}

Result:

Illuminate\Support\Collection Object

(

    [items:protected] => Array

        (

            [India] => 5000

            [US] => 3000

        )

)

Conclusion

In this tutorial, you have learned how to use a group by with a different purpose in laravel eloquent queries.

More Laravel Posts

<div class="gen-info-box">
<b>Recommended:-</b> <a href="/post/laravel-where-like-query-example/" target="_blank">Laravel where Like Query Example</a>
</div>

<div class="gen-info-box">
<b>Recommended:-</b> <a href="/post/laravel-where-null-and-where-not-null-query/" target="_blank">Laravel Where Null and Where Not Null Query</a>
</div>

Please ask in the comments!

Suport : Cotekno.Com

comments powered by Disqus