Laravel Eloquent 更新仅在进行更改时

2021-12-26 00:00:00 php laravel eloquent

如果对记录进行了更改,有没有办法使用 eloquent 模型更新 Laravel 中的记录?我不希望任何用户无缘无故地一遍又一遍地请求数据库,只需点击按钮即可保存更改.我有一个 javascript 功能,可以根据页面中的某些内容是否已更改启用和禁用保存按钮,但我想知道是否可以确保在服务器上执行此类功能边也.我知道我可以自己完成它(意思是:不诉诸框架的内部功能)只是通过检查记录是否有变化,但在这样做之前,我想知道 Laravel eloquent 模型是否已经处理好了这样,我就不需要重新发明轮子了.

Is there any way to update a record in Laravel using eloquent models just if a change has been made to that record? I don't want any user requesting the database for no good reason over and over, just hitting the button to save changes. I have a javascript function that enables and disables the save button according with whether something has changed in the page, but I would like to know if it's possible to make sure to do this kind of feature on the server side too. I know I can accomplish it by myself (meaning: without appealing to an internal functionality of the framework) just by checking if the record has change, but before doing it that way, I would like to know if Laravel eloquent model already takes care of that, so I don't need to re-invent the wheel.

这是我用来更新记录的方式:

This is the way I use to update a record:

$product = Product::find($data["id"]);
$product->title = $data["title"];
$product->description = $data["description"];
$product->price = $data["price"];
//etc (string values were previously sanitized for xss attacks)
$product->save();

推荐答案

你已经做到了!

save() 将检查模型中的某些内容是否已更改.如果没有,它将不会运行数据库查询.

save() will check if something in the model has changed. If it hasn't it won't run a db query.

下面是IlluminateDatabaseEloquentModel@performUpdate中相关部分的代码:

Here's the relevant part of code in IlluminateDatabaseEloquentModel@performUpdate:

protected function performUpdate(Builder $query, array $options = [])
{
    $dirty = $this->getDirty();

    if (count($dirty) > 0)
    {
        // runs update query
    }

    return true;
}

<小时>

getDirty() 方法只是将当前属性与创建模型时保存在 original 中的副本进行比较.这是在 syncOriginal() 方法中完成的:


The getDirty() method simply compares the current attributes with a copy saved in original when the model is created. This is done in the syncOriginal() method:

public function __construct(array $attributes = array())
{
    $this->bootIfNotBooted();

    $this->syncOriginal();

    $this->fill($attributes);
}

public function syncOriginal()
{
    $this->original = $this->attributes;

    return $this;
}

<小时>

如果你想检查模型是否脏,只需调用 isDirty():

if($product->isDirty()){
    // changes have been made
}

或者如果你想检查某个属性:

Or if you want to check a certain attribute:

if($product->isDirty('price')){
    // price has changed
}

相关文章