• <bdo id='WrBjc'></bdo><ul id='WrBjc'></ul>
  • <i id='WrBjc'><tr id='WrBjc'><dt id='WrBjc'><q id='WrBjc'><span id='WrBjc'><b id='WrBjc'><form id='WrBjc'><ins id='WrBjc'></ins><ul id='WrBjc'></ul><sub id='WrBjc'></sub></form><legend id='WrBjc'></legend><bdo id='WrBjc'><pre id='WrBjc'><center id='WrBjc'></center></pre></bdo></b><th id='WrBjc'></th></span></q></dt></tr></i><div id='WrBjc'><tfoot id='WrBjc'></tfoot><dl id='WrBjc'><fieldset id='WrBjc'></fieldset></dl></div>
    <tfoot id='WrBjc'></tfoot>
  • <small id='WrBjc'></small><noframes id='WrBjc'>

    <legend id='WrBjc'><style id='WrBjc'><dir id='WrBjc'><q id='WrBjc'></q></dir></style></legend>

        Laravel Eloquent 更新仅在进行更改时

        时间:2023-09-24

        <small id='YYwGN'></small><noframes id='YYwGN'>

      1. <tfoot id='YYwGN'></tfoot>

          <i id='YYwGN'><tr id='YYwGN'><dt id='YYwGN'><q id='YYwGN'><span id='YYwGN'><b id='YYwGN'><form id='YYwGN'><ins id='YYwGN'></ins><ul id='YYwGN'></ul><sub id='YYwGN'></sub></form><legend id='YYwGN'></legend><bdo id='YYwGN'><pre id='YYwGN'><center id='YYwGN'></center></pre></bdo></b><th id='YYwGN'></th></span></q></dt></tr></i><div id='YYwGN'><tfoot id='YYwGN'></tfoot><dl id='YYwGN'><fieldset id='YYwGN'></fieldset></dl></div>

            <bdo id='YYwGN'></bdo><ul id='YYwGN'></ul>
              <tbody id='YYwGN'></tbody>
                • <legend id='YYwGN'><style id='YYwGN'><dir id='YYwGN'><q id='YYwGN'></q></dir></style></legend>

                • 本文介绍了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 更新仅在进行更改时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Laravel Eloquent:如何仅从连接表中获取某些列 下一篇:更新而不触及时间戳(Laravel)

                  相关文章

                  最新文章

                  <tfoot id='RtKUQ'></tfoot>

                  1. <i id='RtKUQ'><tr id='RtKUQ'><dt id='RtKUQ'><q id='RtKUQ'><span id='RtKUQ'><b id='RtKUQ'><form id='RtKUQ'><ins id='RtKUQ'></ins><ul id='RtKUQ'></ul><sub id='RtKUQ'></sub></form><legend id='RtKUQ'></legend><bdo id='RtKUQ'><pre id='RtKUQ'><center id='RtKUQ'></center></pre></bdo></b><th id='RtKUQ'></th></span></q></dt></tr></i><div id='RtKUQ'><tfoot id='RtKUQ'></tfoot><dl id='RtKUQ'><fieldset id='RtKUQ'></fieldset></dl></div>

                        <bdo id='RtKUQ'></bdo><ul id='RtKUQ'></ul>
                    1. <small id='RtKUQ'></small><noframes id='RtKUQ'>

                    2. <legend id='RtKUQ'><style id='RtKUQ'><dir id='RtKUQ'><q id='RtKUQ'></q></dir></style></legend>