laravel Eloquent push() 和 save() 区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17035682/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 02:30:06  来源:igfitidea点击:

Eloquent push() and save() difference

laravellaravel-4eloquent

提问by Melvin

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

我已经阅读了 laravel 4 文档关于 eloquent 并且对 push() 部分非常感兴趣。它说,

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();

有时,您可能不仅希望保存模型,还希望保存其所有关系。为此,您可以使用 push 方法:

保存模型和关系

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

抱歉,我认为 save() 和 push() 之间的区别有点模糊。我希望有人可以为我清除这个。谢谢你。

回答by Kylie

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

它只是表明push()将更新与相关模型相关的所有模型,因此如果您更改任何关系,则调用push()它将更新该模型及其所有关系,就像这样......

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

然后它将保存所有数据,并将地址保存到 address 中table/model,因为您在User model.

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

push()还将更新您的任何用户/模型的所有相关模型的所有 updated_at 时间戳 push()

Hopefully that will clear the things....

希望这会清除事情......

回答by Antonio Carlos Ribeiro

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();

回答by Alika Matthew

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.

push() 只能用于更新现有模型实例及其关系,而不是创建新模型实例。简单地说:push() 更新而不是插入。