Programming Language Trends to Watch in 2026
For developers deciding what to learn or use next, “most popular” depends on what each ranking counts. A developer survey records what…
After PHP code loads records into a Laravel collection, reduce() processes each record and carries a running value from one record to the next. The final result can be a single number or a structure, such as an array of related totals.
The carried value, called the accumulator, stores the result built so far. The function you give reduce(), called a callback, returns the next accumulator. Laravel passes that value to the callback for the next item. Use reduce() when custom logic needs to update the value as it processes items. For a standard calculation, use a method such as sum().
Laravel runs the reduce() callback, the function you provide, once for each collection item. The callback receives the current accumulator, the item, and the item’s key. Its return value becomes the accumulator for the next call. You can provide an initial value, also called a seed. If you omit the seed, the first accumulator is null (Laravel’s collection documentation).
For example, this reduction adds three numbers:
$total = collect([12, 8, 5])->reduce(
function ($carry, $number) {
return $carry + $number;
},
0
);
The accumulator starts at 0. The first callback adds 12 and returns 12; the next adds 8 and returns 20; the last adds 5 and returns 25. Laravel returns that final value as $total.
The callback must return the updated accumulator so Laravel can pass it to the next call. Changing a local variable without returning it does not pass the new value forward. PHP’s array_reduce() uses the same pattern, but Laravel’s callback also receives the collection key.
An accumulator that holds an array lets one reduction calculate several related results. For example, this code counts paid invoices and adds their totals:
$summary = $invoices->reduce(
function (array $carry, array $invoice): array {
if ($invoice['status'] === 'paid') {
$carry['count']++;
$carry['total'] += $invoice['total'];
}
return $carry;
},
['count' => 0, 'total' => 0]
);
The seed sets the accumulator’s shape and gives both values a starting point. For each paid invoice, the callback updates the count and total, then returns the array. For an unpaid invoice, it returns the array unchanged. The final result is one array with count and total fields:
['count' => 3, 'total' => 1250]
Passing an explicit seed tells the callback which accumulator type to expect. The Laravel documentation shows the same approach for supplying an initial value and accumulating a total (Laravel’s reduce() examples).
reduce() fits calculations where each item updates a running result, especially when one pass needs to produce multiple related values. For instance, the invoice reduction applies the paid-status rule and updates both aggregates during the same traversal.
A chain can express the same work clearly:
$paidInvoices = $invoices->where('status', 'paid');
$summary = [
'count' => $paidInvoices->count(),
'total' => $paidInvoices->sum('total'),
];
This version first builds a filtered collection, then calculates its count and total. The reduction avoids creating that extra collection and updates both values as it goes through the original collection. Laravel collection methods commonly return new collections, so these extra results are one reason a chain can do more work (Laravel collection methods).
That difference does not make reduce() automatically faster. Laravel’s documentation establishes how the methods work, but does not provide benchmarks showing that a custom reduction outperforms collection chains. Choose the version that makes the calculation easiest to understand, and benchmark the actual code if performance matters.
For a standard calculation, use the method that names it. Use sum() to total values or count() to count items instead of writing a reduction that repeats that logic. Laravel documents these collection methods alongside reduce() (Collection API).
If the records are still in a database, consider calculating the result in the query instead of loading every row into PHP. Laravel’s query builder provides aggregate methods such as count, sum, and avg (query builder aggregates). Choose a query based on the results you need and how the database should filter the records.
Give Vroni a GitHub issue, bug report, spec, or rough idea. It reads the repo, plans the change, writes code, runs checks, and works toward a review-ready pull request.
Take a look at vroni.com