I will let you know how to use mysql sum function with select statement in laravel 5. we will use db raw with sum function in laravel. In this article i will leave simple example of laravel select sum group by with db::raw.
If you require to use sql sum() in select statement then you are a right place. If you have to write simple sql query then it is a very simple to write but if you need to use mysql predefine function then you require to use sum() using db raw.
So, basically bellow query i want to count number of address of per user, So let's see how will bellow query.
$data = DB::table("users")
->select(DB::raw("SUM(users_address.id) as total_address"))
I hope you understand and found your best solution....
Do you like below Tutorials ?
- PHP Laravel 5.5 - import export data into excel and csv using maatwebsite
- Laravel 7.x and 6.x create custom facade example from scratch
- Node JS - Get Real time online users counter using socket io example
- PHP Laravel 7.x and 6.x - Bootstrap Admin Theme Integration example from scratch
- MySQL Query - How to select records of today to next Sunday ?
- Laravel 7.x and 6.x - username custom validation example
- Laravel 5.5 - Import Extract zip file using chumper/zipper package
- PHP Laravel Set Custom Validation Error Messages Example