Laravel Eloquent Group By with Multiple Columns Example

By Hardik Savani November 5, 2023 Category : Laravel

Hey Friends,

In this tutorial, you will discover laravel group by multiple columns. if you want to see an example of laravel collection group by two columns then you are in the right place. This tutorial will give you a simple example of laravel eloquent group by multiple columns. let’s discuss about group by multiple columns in laravel. Here, Create a basic example of laravel eloquent group by multiple columns.

Sometimes we may require to add group by with multiple columns, if we have mysql query then we can do it easily by using sql query. But if you want to give multiple columns in groupBy() of Laravel Query Builder then you can give by comma separated values as bellow example.

Example:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use DB;

class UserController extends Controller

{

/**

* Display a listing of the resource.

*

* @return \Illuminate\Http\Response

*/

public function index(Request $request)

{

$data = DB::table("items_count")

->select(

'items_count.*'

,DB::raw("SUM(items_count.quantity) as total_quantity"))

->groupBy('items_count.id_item','items_count.id_cat')

->get();

dd($data);

}

}

I hope it can help you...

Tags :
Shares