Eloquent - Updating all models in a collection
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
Title: Efficiently Updating Attributes Across Multiple Models in Laravel Using Eloquent Collections
Introduction
In your application development journey, you may encounter the need to update attributes for a group of models. A Laravel Eloquent model collection offers an efficient way to manage multiple models at once. However, updating attributes in such collections can be tricky if not done correctly. In this post, we will explore various approaches and best practices to successfully update all models in a collection using Laravel's Eloquent ORM (Object Relational Mapper).
1. Using Mass Assignment on Models
The most convenient way to set attributes for multiple models is through mass assignment. You can update the attribute by simply setting it on each model and then saving them separately using their save() method:
```php
$models = MyModel::findMany([1, 2, 3]);
foreach ($models as $model) {
$model->attribute_name = 'new_value';
$model->save();
}
```
The above code assumes you have a collection of models and their IDs are [1, 2, 3]. The model name is `MyModel`. The attribute to be updated is named `attribute_name`, and its value will be set to 'new_value' for all the models. Keep in mind that mass assignments must follow your model's rules and restrictions.
Advantages:
- Straightforward and easy to implement
- Allows control over each model's status
Disadvantages:
- Requires iterating through the collection, which might be time-consuming for larger collections
2. Updating Multiple Models Using Eloquent Relations
Laravel allows you to define relationships between your models using Eloquent relations. If one of these relationships is a "hasMany" relationship, you can perform bulk operations on the related models through their parent model. Let's say you have a `Parent` class that has the above-mentioned collection and a `Child` class that belongs to many parents:
```php
$parent = Parent::find(1);
$parent->children()->update(['attribute_name' => 'new_value']);
```
In this case, your `Parent` model has a "hasMany" relationship with the `Child` model. You first retrieve the parent and then update the related children models using their update() method, passing an array of attributes to be updated.
Advantages:
- Elegant solution for one-to-many relationships
- Reduces code duplication when working with related models
Disadvantages:
- Limited usability since it depends on the defined relations in your models
3. Using Model Scopes to Update Attributes
Model scopes are a convenient way to define query conditions that can be reused across your application. To update attributes in all the models of a collection using model scopes, you could define a scope within your Eloquent model with the desired updates:
```php
class MyModel extends Model {
protected $scopes = [
'update_attribute' => function($value) {
return $this->update(['attribute_name' => $value]);
},
];
}
```
Now, you can update the attribute for all models in your collection by calling the scope:
```php
MyModel::update_attribute('new_value');
```
Advantages:
- Efficient and reusable solution for updating attributes in collections
- Makes code readable and maintainable when dealing with multiple update operations
Disadvantages:
- Not ideal for one-to-many relationships as it doesn't use the Eloquent relations' power
Conclusion
Updating attributes in a collection of models can be efficiently achieved using different approaches. The best method depends on your application's design and requirements. In most cases, mass assignments and model scopes provide efficient and straightforward solutions for common scenarios. In some situations, using Eloquent relations might come in handy if you have defined relationships between the involved models. Remember to always follow Laravel's best practices and keep your code clean, maintainable, and scalable.