How to group by multiple columns in Laravel Query Builder?

By Hardik Savani | June 12, 2016 | | 73233 Viewer | Category : Laravel


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:

$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();

print_r($data);



Hardik Savani
My name is Hardik Savani. I'm a full-stack developer, entrepreneur and owner of Aatman Infotech. 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, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.
Follow Me: Github Twitter
***Do you want me hire for your Project Work? Then Contact US.


We are Recommending you: