Laravel 9 - How To Select with Sum Query?
Apr 15, 2022 . Admin
This article goes in detailed on how to select with sum query in laravel 9. We will use how to create database column data sum in laravel 9. you can understand a concept of database column sum. We will use sum query.
Most of us mostly need to get the sum of few numbers ,visitor,or others in 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 column by using mysql SUM(). So we have two way to get the sum of any column value.At first one is we can do by use laravel sum() of query builder and another one is do directly with select statement by using DB::raw().
Here I will give you both example. So we can see and also use any one as perfect for us.Download Laravel
Let us begin the tutorial by installing a new laravel application. if you have already created the project, then skip following step.
composer create-project laravel/laravel example-appExample 1
$data = DB::table("users")->sum('student_marks'); print_r($data);Example 2
$data = DB::table("users") ->select(DB::raw("SUM(student_marks) as count")) ->orderBy("created_at") ->groupBy(DB::raw("year(created_at)")) ->get(); print_r($data);
I hope it help you...