Laravel Eloquent 更新仅在进行更改时

Laravel Eloquent update just if changes have been made(Laravel Eloquent 更新仅在进行更改时)
本文介绍了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
}

这篇关于Laravel Eloquent 更新仅在进行更改时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)