Difference between Laravel save() and push()

In Laravel when you insert a data we use save(). We will see the difference between

save() and push with examples.

public function push()

{

if ( ! $this->save()) return false;

// To sync all of the relationships to the database, we will simply spin through

// the relationships and save each model via this "push" method, which allows

// us to recurse into all of these nested relations for the model instance.

foreach ($this->relations as $models)

{

foreach (Collection::make($models) as $model)

{

if ( ! $model->push()) return false;

}

}

return true;

}

push() will update all the models so if you change any relationships then call push so that it will update

the model and all its relations.

$user = User::find(32);

$user->name = "TestUser";

$user->state = "Texas";

$user->location->address = "123 test address"; //This line is a pre-defined

relationship

If you gave $user->save(); the address will not be saved to address model. Instead if

you use $user->push(); the address will be saved to address table/model since you

have defined that relationship in User model. Push() will also update the updated_at

timestamps of all the related models of the user/model you push().