Laravel: Eloquent how to update a model and related models in one go
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
# Laravel Eloquent: How to Update Related Models in One Go â Mastering Nested Updates
As developers working with Object-Relational Mappers (ORMs) like Eloquent, one of the most frequent tasks is managing complex data relationships. When you have nested relationshipsâsuch as updating a parent model and simultaneously modifying data on its related child modelsâthe desire for a single, expressive command becomes very strong.
Today, we are diving into a common challenge: how to efficiently update related models, specifically when dealing with one-to-one relationships, all within a single Eloquent call. Does Eloquent natively support updating nested attributes using dot notation directly in an `update()` method? The short answer is no, not by default. However, we can absolutely craft elegant solutions that achieve this goal, leveraging Eloquent's power to structure data manipulation.
## The Challenge: Nested Updates with Eloquent
Letâs set up a scenario. We have a `User` model and a one-to-one relationship with a `BestFriend` model. We want to change the city for both the user and their best friend simultaneously.
The intuitive approach might look like this:
```php
$user = User::find(1);
// Attempting the desired update structure:
$user->update([
'city' => 'Amsterdam',
'bestfriend.city' => 'Amsterdam' // Does Eloquent understand this path?
]);
```
When attempting the above, standard Eloquent does not automatically traverse nested attributes defined by relation names (like `bestfriend.city`) within a simple update operation. The query builder expects direct column updates unless specific methods are implemented to handle this relational logic.
## The Solution: Custom Mutators for Nested Updates
To unlock this level of convenience, we need to teach Eloquent how to interpret these nested keys. The most robust and clean way to achieve this is by implementing custom accessor or mutator methods within your model. This allows you to intercept the data being set and redirect those changes into the correct related models before persisting the update.
For a one-to-one relationship, we can create a method that handles the nested update logic directly on the parent model.
### Step 1: Define the Relationship and Model Structure
First, ensure your models are correctly set up. Assuming a standard setup where `User` has a `bestFriend` relationship:
```php
// app/Models/User.php
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
public function bestFriend()
{
return $this->hasOne(BestFriend::class);
}
/**
* Custom method to handle nested updates for the best friend's data.
*
* @param array $attributes The attributes being updated.
*/
public function setBestFriendArrayAttribute(array $attributes)
{
// When 'bestfriend.city' is used, it means we need to update the related model.
if (isset($attributes['city'])) {
$this->bestFriend->update(['city' => $attributes['city']]);
}
}
}
```
### Step 2: Executing the Smart Update
With the custom method in place, Eloquent now understands how to process the `bestfriend.city` key when it encounters an update call on the parent model. We can now execute the desired operation cleanly:
```php
$user = User::find(1);
// Now, this single update command handles both models!
$user->update([
'city' => 'Amsterdam',
'bestfriend.city' => 'Amsterdam' // This triggers our custom method!
]);
// Or, if you are using the static approach:
// $user->update([
// 'city' => 'Amsterdam',
// 'bestfriend' => [
// 'city' => 'Amsterdam'
// ]
// ]);
```
This pattern demonstrates how powerful Eloquent is. Instead of writing complex nested SQL queries or manually managing multiple model updates, we leverage the framework to handle the relational logic within our application layer. This approach keeps your code readable and tightly coupled with the data structure you define in your models.
## Conclusion
Updating related models in a single operation requires stepping beyond Eloquent's default behavior. By implementing custom methods like `setBestFriendArrayAttribute`, developers gain fine-grained control over how nested data is persisted. This technique transforms potentially complex relational updates into simple, readable code. As you build larger applications using Laravel, mastering these advanced Eloquent features is key to writing efficient, maintainable, and expressive data access layers. Always remember that the power of the framework lies in how you teach it to behave!