laravel group by – Laravel Group By with Max Value Query Example

Rate this post

Laravel Group By with Max Value Query you need help in getting the max id of a table that is grouped by member_id in laravel.

laravel group by with MAX/GROUP BY Query

The query builder also provides a variety of methods for retrieving aggregate values like count , max , min , avg , and sum .

Example 1: Laravel Group By with Max ID

/**
 * Write code on Method
 * Devloped By www.pakainfo.com
 * @return response()
 */
public function index()
{
    $records = MemberSalary::select('id', 'member_id', DB::raw('MAX(id) as max_id'))
                            ->where('is_reward', 1)
                            ->groupBy('member_id')
                            ->get();
   
    dd($records);
}

Don’t miss : do not run composer as root/super user! see https://getcomposer.org/root for details

Free Live Chat for Any Issue

Example 2: Laravel Group By with Max Date

/**
 * Write code on Method
 * Devloped By www.pakainfo.com
 * @return response()
 */
public function index()
{
    $records = MemberSalary::select('id', 'member_id', DB::raw('MAX(reward_date) as max_reward_date'))
                            ->where('is_reward', 1)
                            ->groupBy('member_id')
                            ->get();
   
    dd($records);
}

Example 3: Laravel Group By with Max Value

/**
 * Write code on Method
 * Devloped By www.pakainfo.com
 * @return response()
 */
public function index()
{
    $records = MemberSalary::select('id', 'member_id', DB::raw('MAX(rank) as max_rank'))
                            ->where('is_reward', 1)
                            ->groupBy('member_id')
                            ->get();
   
    dd($records);
}

Download

I hope you get an idea about laravel group by.
I would like to have feedback on my infinityknow.com.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.