Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eloquent push() and save() difference

I have read laravel 4 docs about eloquent and was quite intrigued by the push() part. It says,

Sometimes you may wish to save not only a model, but also all of its relationships. To do so, you may use the push method:

Saving A Model And Relationships

$user->push();

See link here

Sorry but it's a bit blurry on my part the difference between save() and push(). I am hoping someone can clear this one out for me. Thank you.

like image 347
Melvin Avatar asked Jun 11 '13 02:06

Melvin


People also ask

What is-> save() in Laravel?

save() method is used both for saving new model, and updating existing one. here you are creating new model or find existing one, setting its properties one by one and finally saves in database.

What is difference between save and create in Laravel?

Save can be used to both create a new Record and update a existing record . Whereas create is used to create a new record by providing all required field at one time .

What is the use of eloquent in Laravel?

The Eloquent ORM included with Laravel provides a beautiful, simple ActiveRecord implementation for working with your database. Each database table has a corresponding "Model" which is used to interact with that table. Models allow you to query for data in your tables, as well as insert new records into the table.


3 Answers

Let's say you did this:

$user = User::find(1);

$user->phone = '555-0101';
$user->address->zip_code = '99950';

You just made changes to two different tables, to save them you have to:

$user->save();
$user->address->save();

or

$user->push();
like image 44
Antonio Carlos Ribeiro Avatar answered Sep 30 '22 14:09

Antonio Carlos Ribeiro


Heres the magic behind the scenes...

/**
 * Save the model and all of its relationships.
 *
 * @return bool
 */
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;
}

It just shows that push() will update all the models related to the model in question, so if you change any of the relationships, then call push() It will update that model, and all its relations Like so...

$user = User::find(32);
$user->name = "TestUser";
$user->state = "Texas";
$user->location->address = "123 test address"; //This line is a pre-defined relationship

If here you just...

$user->save();

Then the address wont be saved into the address model.... But if you..

$user->push();

Then it will save all the data, and also save the address into the address table/model, because you defined that relationship in the User model.

push() will also update all the updated_at timestamps of all related models of whatever user/model you push()

Hopefully that will clear the things....

like image 164
Kylie Avatar answered Sep 30 '22 15:09

Kylie


push() can only be used to update an existing model instance along side its relations not to create a new one. Simply say: push() updates and not insert.

like image 23
Alika Matthew Avatar answered Sep 30 '22 14:09

Alika Matthew