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

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

        laravel 挂钩到 Eloquent 前和后保存每个模型

        时间:2023-09-23
      1. <legend id='ETUyF'><style id='ETUyF'><dir id='ETUyF'><q id='ETUyF'></q></dir></style></legend>

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

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

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

                  本文介绍了laravel 挂钩到 Eloquent 前和后保存每个模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我是 Laravel 和 ORM 的新手.我怎么能在保存任何模型之前和之后连接到 Eloquent 来触发代码?我知道我可以对特定模型执行以下操作,但我正在研究如何为每个模型执行此操作.

                  I'm new to Laravel and ORM's in general. How could i hook into Eloquent to fire code before and after a save of any model? I know i can do the following for specific models but i'm looking at figuring out how to do this for every model.

                  class Page extends Eloquent {
                  
                     public function save()
                     {
                        // before save code 
                        parent::save();
                        // after save code
                     }
                  }
                  

                  推荐答案

                  您可以创建一个扩展 eloquent 的 BaseModel 类,然后让您的所有模型扩展 BaseModel.举个例子:

                  You can create a BaseModel class that extends eloquent and then have all your models extend BaseModel. Here's an example:

                  abstract class Elegant extends Eloquent{
                  
                  /* Save ****************************/
                  public function preNew() {}
                  public function postNew() {}
                  public function preSave() { return true; }
                  public function postSave() {}
                  public function save($validate=true, $preSave=null, $postSave=null)
                  {
                      $newRecord = !$this->exists;
                      if ($validate)
                          if (!$this->valid()) return false;
                      if($newRecord)
                          $this->preNew();
                  
                      $before = is_null($preSave) ? $this->preSave() : $preSave($this);
                        // check before & valid, then pass to parent
                      $success = ($before) ? parent::save() : false;
                      if ($success)
                          is_null($postSave) ? $this->postSave() : $postSave($this);
                      if($newRecord)
                          $this->postNew();
                      return $success;
                  }
                  public function onForceSave(){}
                  public function forceSave($validate=true, $rules=array(), $messages=array(), $onForceSave=null)
                  {
                      if ($validate)
                          $this->valid($rules, $messages);
                       $before = is_null($onForceSave) ? $this->onForceSave() : $onForceSave($this);  // execute onForceSave
                       return $before ? parent::save() : false; // save regardless of the result of validation
                  }
                  
                  /** Soft Delete ****************************/
                  public function preSoftDelete() {  return true;  }
                  public function postSoftDelete()  { }
                  public function softDelete($val = true, $preSoftDelete=null, $postSoftDelete=null)
                  {
                      if ($this->exists)
                      {
                          $before = is_null($preSoftDelete) ? $this->preSoftDelete() : $preSoftDelete($this);
                          $success = null;
                          if($before) {
                              $this->set_attribute(static::$softDelete, $val);
                              $success = $this->save(false);
                          }
                          else
                              $success = false;
                          if ($success)
                          {
                              is_null($postSoftDelete) ? $this->postSoftDelete() : $postSoftDelete($this);
                           }
                          return $success;
                      }
                  }
                  
                  /** Hard Delete ****************************/
                  public function preDelete()  { return true;}
                  public function postDelete(){}
                  public function delete( $preDelete=null, $postDelete=null)
                  {
                      if ($this->exists)
                      {
                          $before = is_null($preDelete) ? $this->preDelete() : $preDelete($this);
                          $success = ($before) ? parent::delete() : false;
                          if ($success)
                          {
                              is_null($postDelete) ? $this->postDelete() : $postDelete($this);
                           }
                          return $success;
                      }
                  }
                  }
                  

                  这篇关于laravel 挂钩到 Eloquent 前和后保存每个模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:扩展/覆盖 Eloquent 创建方法 - 不能使静态方法非静 下一篇:Laravel Eloquent Serialization:如何重命名属性?

                  相关文章

                  最新文章

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

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

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

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