How to add changes to Model and its relationships in Laravel
Categorized as Laravel
When making changes to a Model and its relations in Laravel, you don’t need to call save()
method on each of them. Instead, you just can call push()
on the main Model and it will recursively save any changes.
$user = User::first();
$user->vip = true;
$user->profile->notes = 'This user is a valued customer';
// ❌ You could it like this
$user->profile->save();
$user->save();
// ✅ You should do it like this
$user->push();