Most of us most need to get the sum of a few numbers, visitors, or others in the Laravel application. Today now in this post I will show you How to Select with Sum Query in Laravel? We can easily get the sum of columns by using MySQL SUM(). So we have two ways to get the sum of any column value. At first, one is we can do this by using Laravel sum() of the query builder, and another one is done directly with the select statement by using DB::raw().
Here I will give you both examples. So we can see and also use anyone as perfect for us.
Let us begin the tutorial by installing a new Laravel application. if you have already created the project, then skip the following step.
composer create-project laravel/laravel example-app
Example 1 :
$data = DB::table("users")->sum('student_marks');
Example 2 :
$data = DB::table("users")
->select(DB::raw("SUM(student_marks) as count"))
I hope it helps you...