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();
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.
Let's say you did this:
You just made changes to two different tables, to save them you have to:
or
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.
Heres the magic behind the scenes...
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 callpush()
It will update that model, and all its relations Like so...If here you just...
Then the address wont be saved into the address model.... But if you..
Then it will save all the data, and also save the address into the address
table/model
, because you defined that relationship in theUser model
.push()
will also update all the updated_at timestamps of all related models of whatever user/model youpush()
Hopefully that will clear the things....