Laravel Group By with Max Value Query 3 Example

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 Value 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

Example 2: Laravel Group By with Max Date

Laravel Group By with Max Value Query

/**
 * 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);
}

I hope you get an idea about laravel groupby.
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.

Leave a Comment