How to get sum of two different columns with Laravel Query Builder?

How to get sum of two different columns with Laravel Query Builder?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Aggregating Multiple Columns in Laravel Query Builder

When you need to aggregate data from a single row using SQL, combining columns inside an expression is a standard operation. The query SELECT SUM(logins_sun + logins_mon) FROM users_stats WHERE id = 7 correctly returns 1034 because the database engine evaluates the addition first, then applies the aggregation function to the resulting sum. However, when porting this logic directly into Laravel's Query Builder, a common misconception leads to incorrect results.

The specific issue arises from how the sum() method accepts its arguments. In the example where $stats->sum('logins_sun', '+', 'logins_mon') returns 587.0, the developer likely assumed that passing multiple column names separated by a plus sign within the method call would replicate the SQL expression. This approach fails because the sum() helper is designed to accept a single column name as its primary argument. When you pass an array of columns or attempt to chain arguments that imply mathematical addition, Laravel does not construct an SQL string like SUM(col1 + col2). Instead, it typically aggregates only the first valid column provided in the list, ignoring the subsequent parameters intended for the arithmetic operation.

To achieve the desired behavior where logins_sun and logins_mon are added together before being summed, you must construct an expression that mimics the raw SQL logic. The most direct way to do this is using the DB::raw() method. This allows you to inject raw SQL expressions directly into the query builder chain while maintaining Laravel's fluent interface syntax.

You can implement this by chaining a new column definition onto the query object. Here is how you should structure the code:

use Illuminate\Support\Facades\DB;

$stats = DB::table('users_stats')
    ->where('id', 7)
    ->selectRaw('SUM(logins_sun + logins_mon) as total_logins')
    ->first();

In this example, selectRaw() creates a new column alias called total_logins containing the result of your expression. This ensures that Laravel generates the exact SQL statement required: SELECT SUM(logins_sun + logins_mon) AS total_logins FROM users_stats WHERE id = 7. The resulting $stats object will then contain the property total_logins with the value 1034.

If you prefer to avoid creating a new column alias, you can access the raw SQL result directly. However, be aware that accessing columns without aliases often returns them as strings in certain contexts, or requires careful handling of the return type depending on your version of Laravel and the specific accessor being used. Using selectRaw() with an alias is generally the cleaner approach as it integrates seamlessly with Eloquent's accessors if you were to map this data to a model later.

Another consideration involves how you handle the result. Since you are summing numeric values, ensure that your code treats the output as a float or integer. If you chain .sum() after a selectRaw query without an explicit alias, Laravel might not know which column to aggregate if multiple columns are selected. Therefore, explicitly naming the aggregated column prevents ambiguity.

This pattern of using raw expressions is essential whenever the built-in aggregation methods (sum, avg, max, etc.) cannot express your specific mathematical logic. While it requires stepping out of the pure fluent abstraction, DB::raw() keeps you within the Laravel ecosystem without reverting to raw queries that bypass the ORM entirely. This balance allows you to write maintainable code that respects both database capabilities and framework conventions.

Our network