In this tutorial you will learn about the Laravel Group by Example Tutorial and its application with practical example.
In this Laravel group by example tutorial I’ll show you how to use group by in laravel qloquent query. In this tutorial you will 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.
Laravel Group by Example Tutorial
In this tutorial I will guide you through different use case of the laravel group by eloquent query.
- 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
Laravel GroupBy with Collection Example
In this example we will use laravel groupBy with collection.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
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 ) ) ) ) ) |
Laravel Collection Group By Preserve Key
In this example use same example as above but we will pass preserve key as true.
1 |
$collection->groupBy('Key_Name', $preserve_key); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
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 ) ) ) ) ) |
Laravel Collection Group By with Multiple Columns
In this example we will use laravel groupBy with multiple columns.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
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 ) ) ) ) ) |
Laravel Collection Group By with Date
In this example we will use laravel groupBy with date column.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
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 ) ) ) ) ) |
Laravel Collection Group By with Count
In this example we will use laravel groupBy with count method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
Illuminate\Support\Collection Object ( [items:protected] => Array ( [India] => 2 [US] => 2 ) ) |
Laravel Collection Group By with Sum
In this example we will use laravel groupBy with sum method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
Illuminate\Support\Collection Object ( [items:protected] => Array ( [India] => 5000 [US] => 3000 ) ) |