Laravel sum eloquent collection
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
# How to Sum Data in an Eager Loaded Eloquent Collection
As senior developers, we often deal with scenarios where performance and data retrieval efficiency are paramount. When working with Eloquent and eager loading, it's easy to load related models, but aggregating that data—like summing up a count from the related models—requires a thoughtful approach. Simply iterating over the loaded collection might work, but for large datasets, this can lead to inefficient PHP processing.
This post will walk you through the most efficient and idiomatic ways to sum related data when using Eloquent eager loading in Laravel.
## Understanding the Challenge with Eager Loading
You have correctly identified the situation: you eager load `Region` models along with their related `Submit` models, and you want a single total count of `deals` associated with each region.
When you execute `$regions = Region::with('submits')->get();`, Eloquent fetches all the necessary data from the database. The resulting collection is a list of `Region` objects, where each object has a nested collection of `Submit` objects. To get the sum, you must iterate through this structure in PHP and manually calculate the total for each region.
While this approach works, it forces your application to handle the aggregation logic, which is generally less performant than letting the database engine handle the heavy lifting.
## Method 1: Aggregation in PHP (The Direct Approach)
For smaller datasets or when you need complex, conditional summing that relies heavily on application logic, iterating over the collection is straightforward. You can use collection methods like `map` or `reduce` to achieve your goal.
Here is how you would calculate the total deals for each region by iterating over the eagerly loaded data:
```php
public function index()
{
$regions = Region::with('submits')->get();
// Calculate the sum of 'deals' for each region in PHP
$regionsWithDealSum = $regions->map(function ($region) {
// Access the related submits collection and sum their 'deals' property
$total