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

<legend id='DMoHr'><style id='DMoHr'><dir id='DMoHr'><q id='DMoHr'></q></dir></style></legend>
  • <tfoot id='DMoHr'></tfoot>
      • <bdo id='DMoHr'></bdo><ul id='DMoHr'></ul>

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

        在 Eloquent 模型事件中获取先前的属性值

        时间:2023-09-23
          <tbody id='xFVlS'></tbody>
        <tfoot id='xFVlS'></tfoot>

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

              • <small id='xFVlS'></small><noframes id='xFVlS'>

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

                  本文介绍了在 Eloquent 模型事件中获取先前的属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  有没有办法在 savingupdating 事件中查看模型属性的旧/以前值?

                  例如.是否有以下可能:

                  User::updating(function($user){if ($user->username != $user->old->username) doSomething();});

                  解决方案

                  好吧,我是偶然发现的,因为它目前不在文档中...

                  有一个 getOriginal() 方法可以返回原始属性值的数组:

                  User::updating(function($user){if ($user->username != $user->getOriginal('username')) {做一点事();}//如果你需要多个属性,你可以使用://$originalAttributes = $user->getOriginal();//$originalUsername = $originalAttributes['username'];});

                  <块引用>

                  小心,在 Laravel 7 之前 getOriginal 忽略属性类型转换.

                  Is there a way to see the old/previous value of a model's attribute in its saving or updating event?

                  eg. Is something like the following possible:

                  User::updating(function($user)
                  {
                      if ($user->username != $user->old->username) doSomething();
                  });
                  

                  解决方案

                  Ok, I found this quite by chance, as it's not in the documentation at present...

                  There is a getOriginal() method available which returns an array of the original attribute values:

                  User::updating(function($user)
                  {
                      if ($user->username != $user->getOriginal('username')) {
                          doSomething();
                      }
                  
                      // If you need multiple attributes you may use:
                      // $originalAttributes = $user->getOriginal();
                      // $originalUsername = $originalAttributes['username']; 
                  });
                  

                  Be careful, prior to Laravel 7 getOriginal ignores attribute type casting.

                  这篇关于在 Eloquent 模型事件中获取先前的属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Eloquent ORM laravel 5 获取 id 数组 下一篇:Laravel 中的 BelongsTo 和 HasOne 有什么区别

                  相关文章

                  最新文章

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

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