66

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.

Community
  • 1
  • 1
Melvin
  • 5,798
  • 8
  • 46
  • 55

3 Answers3

113

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....

Mr. Pyramid
  • 3,855
  • 5
  • 32
  • 56
Kylie
  • 11,421
  • 11
  • 47
  • 78
  • Thanks for your answer... I just have one issue... About this line: `$user->state->address = "123 test address"; //This line is a pre-defined relationship` What do you mean by this? Is this the same as the relationship you define in eloquent like ->hasOne() ? – Melvin Jun 11 '13 at 04:02
  • Yes thats exactly what I mean :) The hasOne() class extends the Relations class....those are the relations eloquent looks at when it does its push() feature... as can be seen on the line in the code above......foreach ($this->relations as $models) – Kylie Jun 11 '13 at 04:14
  • Thanks Kyle... I just noticed that it won't push() if the related model has no entry on the database. – Melvin Jun 11 '13 at 05:16
  • the `->state` / `->state->address` assignment doesn't make sense. Your example shows `->state` on `$user` is a string, not a relationship. – deefour Jun 20 '13 at 16:32
  • hi @Kylie excellent answer +1. then `push()` "by default" only saves the "direct" relationships of a model, but thanks to the "recursive" iteration that `push()` does (behind the scenes) it manages to save the nested relationships of a model as well, right? –  Mar 22 '23 at 14:34
  • that is, no matter **how nested** the relationships are, right? @Kylie –  Mar 22 '23 at 14:35
44

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();
Antonio Carlos Ribeiro
  • 86,191
  • 22
  • 213
  • 204
  • 1
    @Antonio what about if use push() to insert new relational record instead of edit current record ? is it possible ? thanks in advance. – Dark Cyber Jan 27 '15 at 09:04
  • what if I instantiated $user = new User() and $address = new Address(), and assign $address to $user->address. will push() save both ? – Christopher Francisco May 20 '15 at 16:51
  • 1
    It seems, push() does not work with new records. See this issue: https://github.com/laravel/framework/issues/4641 – JustAMartin Sep 07 '15 at 13:53
4

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.

Alika Matthew
  • 352
  • 4
  • 6