Laravel Collection Sum Multiple Columns in Collection

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Laravel Collection: Summing Multiple Columns Efficiently

As developers working with Laravel, we often leverage the power of Eloquent and the powerful Collections API to handle data manipulation efficiently. Among the many methods available in the Illuminate\Support\Collection, the sum() function is incredibly useful for calculating totals. However, as you’ve discovered, when dealing with nested data—an array of objects or associative arrays—a simple call to sum('column_name') becomes verbose and inefficient if you need to aggregate several fields at once.

This post will dive into how to achieve the goal of summing multiple columns from a Laravel Collection in a single, streamlined operation, avoiding redundant loops and making your code cleaner and more performant.

The Limitation of Simple sum()

The basic usage of sum() works perfectly when you have a simple, flat collection of numbers:

$numbers = collect([10, 5, 20]);
$total = $numbers->sum(); // Returns 35

When you deal with complex collections containing associative arrays, like the one you described, using sum() requires iterating over the collection multiple times if you want totals for different columns:

$collection = collect([
    ['name' => 'Item A', 'pages' => 176, 'price' => 100.00],
    ['name' => 'Item B', 'pages' => 150, 'price' => 250.00],
]);

// Inefficient approach: Two separate iterations
$totalPages = $collection->sum('pages');
$totalPrice = $collection->sum('price');

// This works, but it involves two separate passes over the data.

While this approach is functional, it violates the principle of writing efficient code. We want to calculate all necessary aggregates in a single pass over the data.

The Efficient Solution: Mastering reduce()

To aggregate multiple values from different keys within a collection into a single result, the most powerful and idiomatic tool in the Laravel Collection API is the reduce() method. This method allows you to iterate over the entire collection once and accumulate a desired value based on the current item's properties.

By using reduce(), we can define custom logic that sums up values from multiple columns simultaneously within that single iteration.

Here is how you can calculate the total pages and total price in one go:

$collection = collect([
    ['name' => 'JavaScript: The Good Parts', 'pages' => 176, 'price' => 100.00],
    ['name' => 'JavaScript: The Definitive Guide', 'pages' => 150, 'price' => 250.00],
]);

$totals = $collection->reduce(function ($carry, $item) {
    $carry['total_pages'] += $item['pages'];
    $carry['total_price'] += $item['price'];
    return $carry;
}, [
    'total_pages' => 0,
    'total_price' => 0
]);

// The result is a single associative array containing both sums
print_r($totals);
/*
Array
(
    [total_pages] => 326 // 176 + 150
    [total_price] => 350.00 // 100.00 + 250.00
)
*/

Alternative: Summing Specific Keys via map and Final Aggregation

If you prefer a slightly more functional approach, you can use map() to extract the specific values for each column first, and then use the standard sum() method on those newly created collections. While this involves creating intermediate collections, it can be very readable:

$collection = collect([
    ['name' => 'JavaScript: The Good Parts', 'pages' => 176, 'price' => 100.00],
    ['name' => 'JavaScript: The Definitive Guide', 'pages' => 150, 'price' => 250.00],
]);

$total_pages = $collection->pluck('pages')->sum();
$total_price = $collection->pluck('price')->sum();

echo "Total Pages: " . $total_pages . "\n";   // Output: Total Pages: 326
echo "Total Price: " . $total_price . "\n"; // Output: Total Price: 350

While this method is highly readable, the reduce() approach is generally favored in scenarios where you need to calculate multiple aggregates from a single source, as it guarantees a single pass through the data structure. For complex aggregations within Laravel applications, understanding these advanced Collection methods is key to writing high-performance code, much like leveraging Eloquent relationships on https://laravelcompany.com.

Conclusion

To efficiently sum multiple columns in a Laravel Collection, avoid calling sum() repeatedly. Instead, embrace the functional power of reduce(). By using reduce(), you consolidate the calculation for all required sums into a single iteration, leading to cleaner code and better performance, especially as your data sets grow larger. Choose the method that best fits your readability preference, but always prioritize efficiency when dealing with Laravel Collections.