Laravel db raw with sum of column example


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.

Example:

$data = DB::table("users")

->select(DB::raw("SUM(users_address.id) as total_address"))

->leftjoin("users_address","users_address.user_id","=","users.id")

->groupBy("users.id")

->get();

print_r($data);

I hope you understand and found your best solution....


December 27, 2017 | 1574 Viewer | Category : PHP Laravel
Related Posts