ItSolutionStuff.com

Laravel Eloquent Group By with Multiple Columns Example

By Hardik Savani β€’ November 5, 2023
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: Laravel
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

πŸ“Ί Subscribe on YouTube

We Are Recommending You

β˜…

How to Get Last Executed Query in Laravel 10?

Read Now β†’
β˜…

How to Use DB Raw Query in Laravel?

Read Now β†’
β˜…

Laravel Where Clause with Function Query Example

Read Now β†’
β˜…

Laravel Eloquent whereRelation() Condition Example

Read Now β†’
β˜…

Laravel Eloquent whereHas() Condition Example

Read Now β†’
β˜…

Laravel Eloquent Left Join Where Null Condition Example

Read Now β†’
β˜…

Laravel Eloquent whereNotNull() Query Example

Read Now β†’
β˜…

Laravel Eloquent whereNull() Query Example

Read Now β†’
β˜…

Laravel Where Condition with Two Columns Example

Read Now β†’
β˜…

Laravel Groupby Having with DB::raw() Example

Read Now β†’
β˜…

Laravel Group By with Month and Year Example

Read Now β†’
β˜…

Laravel Select with Count Query with Group By Example

Read Now β†’
β˜…

Laravel Join with Subquery in Query Builder Example

Read Now β†’