Laravel Group By with Min Value Query Example
This post will give you example of laravel group by with min value. you can understand a concept of laravel group by min value. This article goes in detailed on laravel group by min(id). let’s discuss about laravel collection group by min.
If you want to get records with group by query and you need to get minimum date, min id, min value or min number then how you will do it. generally group by get first record, but if you need min value records then i will show you some simple query example and you can fix your problem.
you can use this query in laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 version.
let's see some example query:
Example 1: Laravel Group By with Min ID
/**
* Write code on Method
*
* @return response()
*/
public function index()
{
$data = UserPayment::select('id', 'user_id', DB::raw('MIN(id) as min_id'))
->where('is_paid', 1)
->groupBy('user_id')
->get();
dd($data);
}
Example 2: Laravel Group By with Min Date
/**
* Write code on Method
*
* @return response()
*/
public function index()
{
$data = UserPayment::select('id', 'user_id', DB::raw('MIN(paid_date) as min_paid_date'))
->where('is_paid', 1)
->groupBy('user_id')
->get();
dd($data);
}
Example 3: Laravel Group By with Min Value
/**
* Write code on Method
*
* @return response()
*/
public function index()
{
$data = UserPayment::select('id', 'user_id', DB::raw('MIN(amount) as min_amount'))
->where('is_paid', 1)
->groupBy('user_id')
->get();
dd($data);
}
i hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Laravel Eloquent Group By Year with Sum Example
- Laravel Group By Date and Sum Example
- Laravel Eloquent Group By with Month and Year Example
- Laravel Eloquent Group By Example
- Laravel Eloquent whereNotNull() Query Example
- How to Group By with Order By Desc in Laravel?
- Laravel Collection GroupBy with Examples
- Laravel Where Clause with date_format() Example
- Laravel Group By with Month and Year Example
- Laravel Eloquent Group By with Multiple Columns Example
- Laravel Select with Count Query with Group By Example