Laravel Sum with where and alias with Eloquent

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Mastering Aggregation in Laravel: Replacing Raw SQL Sums with Eloquent Power

Migrating complex database operations from older systems, like those built with raw SQL queries found in legacy projects, into a modern Object-Relational Mapper (ORM) like Laravel’s Eloquent can seem daunting. You've encountered a common scenario: needing to perform conditional aggregations—like calculating sums based on specific WHERE clauses and assigning custom aliases—which feels clumsy when sticking strictly to Eloquent methods.

This post will demonstrate how to achieve the exact results of your legacy SQL aggregation queries using idiomatic Laravel techniques, proving that Eloquent and the Query Builder are powerful enough to handle complex data manipulation without resorting to raw SQL whenever possible.

The Challenge: From Raw SQL to Eloquent Logic

The SQL you provided attempts to calculate conditional sums and assign aliases:

SELECT SUM(source = 'FBS') AS facebook
SELECT SUM(source = 'FBS' AND promotion = 0) AS facebook_promotion
SELECT SUM(source = 'IG') AS instagram
SELECT SUM(source = 'LP') AS landing_page;

While this syntax is common in some database dialects, correctly implementing conditional summing often requires using conditional logic within the SUM() function (e.g., using CASE statements) or leveraging grouping. The goal here is to aggregate data based on distinct categories defined in the source column.

The core challenge is translating this multi-faceted aggregation into a clean, readable Eloquent query.

Solution 1: The Power of Conditional Aggregation with DB::raw

When dealing with highly specific aggregations that require complex conditional logic (like summing based on multiple conditions simultaneously), the most direct path in Laravel is often using the Query Builder's raw expression capabilities (DB::raw). This allows you to inject the exact SQL logic while keeping the execution within the robust Laravel framework.

For your specific requirements, we can use a CASE statement inside the SUM() function to achieve the conditional counting:

use Illuminate\Support\Facades\DB;

// Assuming you are querying the 'data' table
$results = DB::table('data')
    ->select(
        DB::raw("SUM(CASE WHEN source = 'FBS' THEN 1 ELSE 0 END) AS facebook"),
        DB::raw("SUM(CASE WHEN source = 'FBS' AND promotion = 0 THEN 1 ELSE 0 END) AS facebook_promotion"),
        DB::raw("SUM(CASE WHEN source = 'IG' THEN 1 ELSE 0 END) AS instagram"),
        DB::raw("SUM(CASE WHEN source = 'LP' THEN 1 ELSE 0 END) AS landing_page")
    )
    ->first();

// $results will now contain the aggregated sums in a single row.

Why this approach works:

This method directly translates your desired conditional logic into a database-optimized expression. By using CASE WHEN ... THEN 1 ELSE 0 END, we instruct the database to count only rows that match our specific criteria for each alias, effectively performing the required conditional summation in one pass. This keeps the heavy lifting on the database engine while utilizing Laravel's facade to execute the query efficiently.

Solution 2: Grouping and Conditional Summation (Alternative Approach)

If your goal was simply to get the total count for each source category, a more Eloquent-native approach using groupBy would be cleaner. However, since you need specific sums tied to specific conditions (promotion = 0), the conditional aggregation above is superior.

If you were looking to aggregate all sources together:

$totals = DB::table('data')
    ->select('source', DB::raw('COUNT(*) as count'))
    ->groupBy('source')
    ->get();

// This would give you totals like:
// source: 'FBS', count: 150
// source: 'IG', count: 80

Conclusion: Embracing Eloquent's Flexibility

The transition from raw SQL to an ORM is less about replacing all database interaction with model methods and more about understanding the abstraction layer. While Eloquent excels at fetching, creating, and updating models, complex analytical queries often require the specific power of the Query Builder, especially when dealing with advanced features like conditional aggregation.

By mastering techniques like using DB::raw for conditional expressions, you gain the flexibility to write highly optimized, readable, and maintainable code in Laravel. As championing clean architecture, understanding how to leverage the underlying database capabilities—as demonstrated by the rich ecosystem provided by Laravel—is key to becoming a senior developer. Avoid unnecessary abstraction when powerful SQL features are needed; know when to use raw power effectively!