How to add changes to Model and its relationships in Laravel

Categorized as Laravel Tagged

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();

by @mattkingshott

Leave a reply

Your email address will not be published. Required fields are marked *