Today, i we will show you laravel 8 eloquent group by example. This article will give you simple example of laravel 8 eloquent group by example. you will learn laravel 8 eloquent group by example.
how to use laravel 8 group with raw, laravel group by multiple, laravel 8 group by date, laravel 8 group by sum, laravel 8 group by month and laravel 8 collection group by count.
So let’s follow few step to create example of laravel 8 eloquent group by example.
Laravel 8 Eloquent Group By Example
- GroupBy with Collection Example
- Collection Group By Preserve Key
- Collection Group By with Multiple Columns
- Collection Group By with Date
- Collection Group By with Count
- Collection Group By with Sum
Step 1:- 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);
}
Output:
Illuminate\Support\Collection {#294 ▼
#items: array:2 [▼
"India" => Illuminate\Support\Collection {#291 ▼
#items: array:2 [▼
0 => array:4 [▼
"id" => 1
"name" => "Rahul"
"city" => "Mumbai"
"country" => "India"
]
1 => array:4 [▼
"id" => 3
"name" => "Ronak"
"city" => "Gujarat"
"country" => "India"
]
]
}
"US" => Illuminate\Support\Collection {#293 ▼
#items: array:2 [▼
0 => array:4 [▼
"id" => 2
"name" => "Sumit"
"city" => "New York"
"country" => "US"
]
1 => array:4 [▼
"id" => 4
"name" => "Harish"
"city" => "New York"
"country" => "US"
]
]
}
]
}
Step 2:- Collection Group By 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);
}
Output:
Illuminate\Support\Collection {#294 ▼
#items: array:2 [▼
"India" => Illuminate\Support\Collection {#291 ▼
#items: array:2 [▼
"first" => array:4 [▼
"id" => 1
"name" => "Rahul"
"city" => "Mumbai"
"country" => "India"
]
"third" => array:4 [▼
"id" => 3
"name" => "Ronak"
"city" => "Gujarat"
"country" => "India"
]
]
}
"US" => Illuminate\Support\Collection {#293 ▼
#items: array:2 [▼
"second" => array:4 [▼
"id" => 2
"name" => "Sumit"
"city" => "New York"
"country" => "US"
]
"fourth" => array:4 [▼
"id" => 4
"name" => "Harish"
"city" => "New York"
"country" => "US"
]
]
}
]
}
Step 3:- 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);
}
Output:
Illuminate\Support\Collection {#295 ▼
#items: array:3 [▼
"IndiaMumbai" => Illuminate\Support\Collection {#291 ▼
#items: array:1 [▼
0 => array:4 [▼
"id" => 1
"name" => "Rahul"
"city" => "Mumbai"
"country" => "India"
]
]
}
"USNew York" => Illuminate\Support\Collection {#293 ▼
#items: array:2 [▼
0 => array:4 [▼
"id" => 2
"name" => "Sumit"
"city" => "New York"
"country" => "US"
]
1 => array:4 [▼
"id" => 4
"name" => "Harish"
"city" => "New York"
"country" => "US"
]
]
}
"IndiaGujarat" => Illuminate\Support\Collection {#294 ▼
#items: array:1 [▼
0 => array:4 [▼
"id" => 3
"name" => "Ronak"
"city" => "Gujarat"
"country" => "India"
]
]
}
]
}
Step 4:- Collection Group By with Date
public function index()
{
$collection = collect([
['id'=>1, 'name'=>'Rahul', 'created_at' => '2021-06-02 15:15:00'],
['id'=>2, 'name'=>'Sumit', 'created_at' => '2021-07-02 15:16:00'],
['id'=>3, 'name'=>'Ronak', 'created_at' => '2021-06-02 15:18:00'],
['id'=>4, 'name'=>'Harish', 'created_at' => '2021-07-02 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);
}
Output:
Illuminate\Support\Collection {#291 ▼
#items: array:2 [▼
"06/02/2021" => Illuminate\Support\Collection {#293 ▼
#items: array:2 [▼
0 => array:3 [▼
"id" => 1
"name" => "Rahul"
"created_at" => "2021-06-02 15:15:00"
]
1 => array:3 [▼
"id" => 3
"name" => "Ronak"
"created_at" => "2021-06-02 15:18:00"
]
]
}
"07/02/2021" => Illuminate\Support\Collection {#294 ▼
#items: array:2 [▼
0 => array:3 [▼
"id" => 2
"name" => "Sumit"
"created_at" => "2021-07-02 15:16:00"
]
1 => array:3 [▼
"id" => 4
"name" => "Harish"
"created_at" => "2021-07-02 15:10:00"
]
]
}
]
}
Step 5:- Collection Group By with Count
public function index()
{
$collection = collect([
['id'=>1, 'name'=>'Rahul', 'country'=>'India', 'created_at' => '2021-06-02 15:15:00'],
['id'=>2, 'name'=>'Sumit', 'country'=>'India', 'created_at' => '2021-06-02 15:16:00'],
['id'=>3, 'name'=>'Ronak', 'country'=>'Us','created_at' => '2021-06-01 15:18:00'],
['id'=>4, 'name'=>'Harish', 'country'=>'Us', 'created_at' => '2021-06-01 15:10:00'],
]);
$grouped = $collection->groupBy('country')->map(function ($row) {
return $row->count();
});
dd($grouped);
}
Output:
Illuminate\Support\Collection {#295 ▼
#items: array:2 [▼
"India" => 2
"Us" => 2
]
}
Step 6:- Collection Group By with Sum
public function index()
{
$collection = collect([
['id'=>1, 'name'=>'Rahul', 'country'=>'India', 'amount'=>'100', 'created_at' => '2021-06-02 15:15:00'],
['id'=>2, 'name'=>'Sumit', 'country'=>'India', 'amount'=>'200', 'created_at' => '2021-06-02 15:16:00'],
['id'=>3, 'name'=>'Ronak', 'country'=>'Us', 'amount'=>'300', 'created_at' => '2021-06-01 15:18:00'],
['id'=>4, 'name'=>'Harish', 'country'=>'Us', 'amount'=>'100', 'created_at' => '2021-06-01 15:10:00'],
]);
$grouped = $collection->groupBy('country')->map(function ($row) {
return $row->sum('amount');
});
dd($grouped);
}
Output:
Illuminate\Support\Collection {#295 ▼
#items: array:2 [▼
"India" => 300
"Us" => 400
]
}