How to use groupby having with DB::raw in Laravel Query Builder?By Hardik Savani | July 6, 2016 | | 5317 Viewer | Category : Laravel
We most probably require to use group by having because if you work on small project then it's not need to use generally. But if you work with big project like e-commerce, social or ERP level project then you may require to use having clause.
having clause, we can use simply if we have to compare with number or static value like as bellow:
But we need to compare with 10 instead of column name then it can't directly use column name. At that time we should use DB::raw() with column name.
In this example i want to show minimum quantity items, I have two tables one items and another one items_count both mysql table layout is here:
I need to get that product has less quantity to min_quantity of items table, so we check get that items using bellow query:
$items = DB::table("items")
,DB::raw('SUM(items_count.quantity) as total_quantity'))
You will find bellow output get only items no. 2.
 => stdClass Object
[id] => 2
[title] => Itsolutionstuff.com
[min_quantity] => 10
[total_quantity] => 5
- How to merge two array with same keys without loop in PHP ?
- How to solve 'There are no commands defined in the "cashier" namespace' in Laravel 5
- Laravel 5.4 New Feature - Add new Components & Slots in Blade Template
- How to get last record of database table in Laravel?
- Laravel 5 - generate PDF from html view file and download using dompdf
- Laravel 5 - Image Upload and Resize Example using Intervention Image Package
- Laravel 5.3 - How to create SEO friendly sluggable URL
- How to get site URL in Laravel 5?