Laravel Eloquent Group By with Multiple Columns Example
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...
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
- How to Get Last Executed Query in Laravel 10?
- How to Use DB Raw Query in Laravel?
- Laravel Where Clause with Function Query Example
- Laravel Eloquent whereRelation() Condition Example
- Laravel Eloquent whereHas() Condition Example
- Laravel Eloquent Left Join Where Null Condition Example
- Laravel Eloquent whereNotNull() Query Example
- Laravel Eloquent whereNull() Query Example
- Laravel Where Condition with Two Columns Example
- Laravel Groupby Having with DB::raw() Example
- Laravel Group By with Month and Year Example
- Laravel Select with Count Query with Group By Example
- Laravel Join with Subquery in Query Builder Example