Eloquent - Updating all models in a collection

Eloquent - Updating all models in a collection

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

Stefan Bogdanescu

Founder & Senior Architect

Eloquent - Updating all models in a collection

When you retrieve a batch of records using MyModel::findMany(), you end up with an instance of Illuminate\Database\Eloquent\Collection. It is tempting to treat this collection like a simple array and call methods directly on it, such as $models->update(['att' => 'foo']). While Eloquent models themselves have robust update logic, the Collection class does not implement an update method. Attempting to invoke it results in a Call to undefined method Illuminate\Database\Eloquent\Collection::update() error.

This limitation exists because Collection is designed to represent a set of data that has been hydrated into model instances. The primary purpose of these instances is querying and mutation, not bulk persistence. To perform a mass update where you want to modify an attribute on every item in the collection simultaneously, you generally have two distinct paths: iterating through the models individually or utilizing the Query Builder's mass assignment capabilities directly on the raw data.

The most straightforward approach that avoids writing custom SQL is leveraging the each method combined with the model's save() logic. However, this approach carries significant performance implications. If your collection contains hundreds of rows, calling save() on each model triggers individual database transactions. This means you are executing N separate queries instead of a single UPDATE statement. In high-traffic applications, this can lead to connection pool exhaustion or severe latency spikes, especially if the models have many dirty attributes or complex relationships that need to be touched during the save cycle.

To optimize this operation while staying within the Eloquent domain, you must explicitly instruct each model instance to update only the specific attributes intended for change. You can achieve this by using the fill method on each model before saving, which ensures that only the necessary columns are marked as dirty. This prevents the database from overwriting existing data with default values or unrelated fields.

foreach ($models as $model) {
    $model->att = 'foo';
    
    // Explicitly save only the changed attribute to avoid unnecessary queries
    $model->touchOnly(['att'])->save(); 
}

Note that touchOnly is a relatively recent addition to Eloquent, available in Laravel 9 and later. If you are using an older version, you must rely on the standard save() method, accepting the overhead of updating all dirty fields. Additionally, if you have previously used $model->wasChanged('att'), be aware that simply assigning a value sets the model as dirty for that attribute, but calling touchOnly ensures the SQL generated targets exactly what is needed.

If your collection has many items and performance becomes a concern, switching to the Query Builder is often the pragmatic choice. This aligns with how you would handle this scenario in plain SQL: updating based on an IN clause of primary keys. The code becomes significantly more concise and efficient.

$ids = $models->pluck('id'); // Extract IDs from the collection instances
MyModel::whereIn('id', $ids)->update(['att' => 'foo']);

This single query executes in a fraction of the time required to iterate and save each model individually. It also bypasses model event listeners like updating, updated, or saved that might fire on every individual row during an iteration. This is a crucial distinction: if you rely on those lifecycle hooks for auditing or side effects, the Query Builder approach will silently skip them unless you handle the logic manually.

Ultimately, the decision rests on your specific needs. If data integrity and triggering model events are paramount and the dataset is small, iterating with careful attribute management is the Eloquent way. If performance and simplicity are the priority, the Query Builder's mass update capability provides a robust solution that mirrors standard SQL behavior without requiring raw DB facades or custom queries.

For deeper insights into optimizing database interactions in Laravel, you can refer to resources like https://laravelcompany.com.

Our network