Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks
Laravel sum in eloquent query

Laravel sum in eloquent query with example

Aman Jain, January 23, 2022November 10, 2023

In this article we will learn to use aggregate function sum in laravel eloquent and query builder. Laravel itself provide inbuilt method to sum the columns like in MySQL or SQL based database we can do sum of columns using sum aggregate function.

Sum method will give you the sum of all rows in query by providing the column name. For example if you have 10 rows and you want the sum of a column X then you will get the sum of that column from 10 rows

Laravel eloquent or query builder sum method do the same as aggregate function. Laravel sum accepts one parameter as column name.

Here is the syntax

Model::sum(COLUMN_NAME);

//or

Model::select(\DB::raw("SUM(COLUMN_NAME), other_columns "))->get(); // or first() 

//or

\DB::table('table_name')->sum(COLUMN_NAME);

//or

\DB::table('table_name')->select(\DB::raw("SUM(COLUMN_NAME), other_columns "))->first()

In the above syntax we have used 4 examples to show all possibilities to use sum aggregate function.

First is using the Model and sum method of eloquent. Second is Model with select and Db::raw class.

In the third example we have used Query Builder DB class and table function to use sum method of it.

And final example is query builder with select and db raw to use sum method of MySQL or SQL.

Result of above query will be:

select SUM(column_name) from  `table`  

//OR

select SUM(column_name), other_columns from `table`
 

Let’s understand laravel sum in eloquent query with example

Example 1 – Laravel sum aggregate function query using eloquent sum method

So in this example i will use laravel query builder or eloquent sum method to do the sum of column.

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\User;
  
class ArticleController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $Article = Article::sum('like_count');
       \\or
       DB::table('articles')->sum('like_count');
   }
}

Output will be :

select sum(`like_count`) from `articles`

Example 2 – Laravel sum query using raw query

In this example i will use laravel query builder DB::raw method with select to sum the column

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\User;
  
class ArticleController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $Article = Article::select(\DB::raw('sum(like_count), aticles.*'))->get();
     
       \\or
        $Article = Article::selectRaw( 'sum(like_count), aticles.*')->get();
     
      \\OR
       DB::table('articles')->select(\DB::raw('sum(like_count), aticles.*'))->first();
   }
}

In the above example we have used articles table and created a model in our laravel application. Then we imported the necessary classes and used the sum method.

Output will be :

select sum(`like_count`), `aticles`.`*` from `articles`

Also Read : How to use laravel count rows with example ?

Related

Php Laravel laravelmysqlsum

Post navigation

Previous post
Next post

Related Posts

Php How to Filter Data Using Relational Model in Laravel

How to Filter Data Using Relational Model in Laravel ?

June 29, 2022July 1, 2022

In this article i will show you to filter data using the relational model in laravel. Using the laravel relationship between the models we can easily fetch relative data but sometimes we want to fetch the data by filtering the child model that affect the result of parent model. You…

Read More
Laravel How to rollback migration laravel

How to Rollback Migration Laravel ?

March 6, 2022February 8, 2024

In our recent article I showed you about creating migration in laravel and sometimes after creating migration we want to rollback migration laravel so in this article i will show you to rollback migration in laravel. Laravel artisan have multiple options to rollback the migration like we can rollback all…

Read More

How to use if else condition in Laravel 8 blade file ?

December 17, 2021January 18, 2022

In this tutorial i will show you to use if else, if and else if in Laravel blade. Laravel use blade engine to render the template and blade have rich features to use conditional statements in file. So here i will show you to use @if, @elseif and @else in…

Read More

Aman Jain
Aman Jain

With years of hands-on experience in the realm of web and mobile development, they have honed their skills in various technologies, including Laravel, PHP CodeIgniter, mobile app development, web app development, Flutter, React, JavaScript, Angular, Devops and so much more. Their proficiency extends to building robust REST APIs, AWS Code scaling, and optimization, ensuring that your applications run seamlessly on the cloud.

Categories

  • Angular
  • CSS
  • Dart
  • Devops
  • Flutter
  • HTML
  • Javascript
  • jQuery
  • Laravel
  • Laravel 10
  • Laravel 11
  • Laravel 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • August 2025
  • July 2025
  • June 2025
  • May 2025
  • April 2025
  • October 2024
  • July 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • July 2023
  • March 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Recent Posts

  • The Transformative Power of Education in the Digital Age
  • Understanding High Vulnerabilities: A Closer Look at the Week of July 14, 2025
  • Exploring Fresh Resources for Web Designers and Developers
  • The Intersection of Security and Technology: Understanding Vulnerabilities
  • Mapping Together: The Vibrant Spirit of OpenStreetMap Japan
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version