Save Models and Relationships at the Same Time Laravel

Hey Artisan Lover, i am going to show you an amazing tips of Laravel. Do you know that we can update model column value and its relationship column at the same time in Laravel? 

So in this example of save models and relationships at the same time laravel you can learn it. I will share the query of update or insert and the relationship between those. So let's see how we can do it.

We will use push() method to do it. We can do it only using the push method. So let's see how to use push() in Laravel query.


class User extends Model
    public function phone()
        return $this->hasOne('App\Phone');


And then in your controller you have use push() method to do it.

$user = User::first();
$user->name = "Peter";

$user->phone->number = '1234567890';

$user->push(); // This will update both user and phone record in DB


So if you change any of the relationships, then call push() It will update that model, and all its relations Like so. Hope it can help you.