Eloquent collections: each vs foreach

Eloquent collections: each vs foreach

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

Stefan Bogdanescu

Founder & Senior Architect

Eloquent Collections: each vs foreach

When working with Laravel, you often find yourself iterating over an Eloquent query result wrapped in an Illuminate\Support\Collection. While both $collection->each() and a standard foreach loop traverse the underlying data, they are not interchangeable. Understanding the distinction between them is crucial for writing performant code and avoiding subtle bugs related to state mutation.

Internal Iterators vs. Direct Traversal

The most significant difference lies in how each method interacts with the collection's internal state. A standard foreach loop iterates directly over the collection's items using a native PHP iterator. It is essentially a pass-through operation; it does not care what you do with the item, nor does it invoke any specific collection methods on the data during the iteration itself (unless you explicitly call them).

In contrast, $collection->each() delegates the iteration to an internal callback mechanism designed specifically for this purpose. When you use each(), Laravel wraps your closure in a way that allows the collection to intercept calls to count(). This is a common source of confusion. If you call count($collection) inside your each() closure, it will return the original count of the collection, not the current index or a modified count based on mutations within the loop.

Mutation Pitfalls and State Management

This behavior regarding count() becomes critical when mutating the collection during iteration. Consider a scenario where you are removing items from a list as you iterate through it.

// Dangerous: Modifying the collection inside foreach can yield unexpected results
foreach ($collection as $item) {
    if (!$item->shouldBeProcessed()) {
        $collection->remove($item); 
    }
}

While modifying a collection inside a foreach is generally discouraged in PHP, each() provides a slightly different contract. Because each() manages the iteration state internally, it expects the collection to remain static for the duration of the loop. If you mutate the collection (adding or removing items) inside an each() closure, the internal iterator may behave unpredictably depending on the underlying driver implementation.

However, there is a specific edge case often missed: using count() inside each(). If you rely on count() to determine how many iterations have occurred or to check for emptiness within the loop, foreach will reflect the dynamic state of the collection (if it's an array), whereas each() might return a stale count if the underlying implementation caches it.

When to Use Which Method

Use foreach for Direct Access and Mutations

If your goal is simple iteration where you might need to access keys, perform mutations on the underlying array directly, or rely on dynamic state changes (like filtering out items), foreach is the safer choice. It adheres to standard PHP iterator protocols and gives you full visibility into the current state of the data structure.

// Safe for most iteration needs
foreach ($collection as $key => $item) {
    // Direct access to key and value
    if ($item->status === 'deleted') {
        continue; 
    }
    
    // Process item
    $item->save();
}

Use each() for Functional Transformations

The primary use case for $collection->each() is when you want to execute a side effect or a transformation without needing the key-value pair structure provided by foreach. It aligns with functional programming patterns where you treat the collection as an object that can perform actions.

Furthermore, each() is often more readable when chaining other collection methods, as it integrates seamlessly into the fluent interface Laravel provides. However, if you need to return a modified collection or use the result of your closure logic, each() will not help you; you would still need a separate method like map().

Performance Considerations

In terms of raw performance, there is negligible difference between the two for simple read operations. However, if you are iterating over a massive dataset and performing complex logic inside the closure, the overhead of invoking a PHP function for every item in each() versus the direct loop in foreach might become measurable. Generally, foreach is preferred for tight loops where performance is paramount because it avoids the abstraction layer that each() introduces.

Ultimately, choose foreach when you need to treat the collection as a standard PHP array iterator, and choose each() when you are leveraging Laravel's fluent interface for functional-style operations on the dataset.

Our network