久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

  • <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>
      1. <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 class="qwawimqqmiuu" id='ETUyF'><tfoot id='ETUyF'></tfoot><dl id='ETUyF'><fieldset id='ETUyF'></fieldset></dl></div>

        laravel 掛鉤到 Eloquent 前和后保存每個模型

        laravel hook into Eloquent pre and post save for every model(laravel 掛鉤到 Eloquent 前和后保存每個模型)

      2. <small id='QGsFx'></small><noframes id='QGsFx'>

      3. <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 class="qwawimqqmiuu" 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>
                  <tbody id='QGsFx'></tbody>

                  <tfoot id='QGsFx'></tfoot>

                  本文介紹了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模板網!

                  【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                  相關文檔推薦

                  Deadlock exception code for PHP, MySQL PDOException?(PHP、MySQL PDOException 的死鎖異常代碼?)
                  PHP PDO MySQL scrollable cursor doesn#39;t work(PHP PDO MySQL 可滾動游標不起作用)
                  PHP PDO ODBC connection(PHP PDO ODBC 連接)
                  Using PDO::FETCH_CLASS with Magic Methods(使用 PDO::FETCH_CLASS 和魔術方法)
                  php pdo get only one value from mysql; value that equals to variable(php pdo 只從 mysql 獲取一個值;等于變量的值)
                  MSSQL PDO could not find driver(MSSQL PDO 找不到驅動程序)

                  <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 class="qwawimqqmiuu" id='owFIG'><tfoot id='owFIG'></tfoot><dl id='owFIG'><fieldset id='owFIG'></fieldset></dl></div>

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

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

                          1. 主站蜘蛛池模板: 亚洲乱码国产乱码精品精98午夜 | 亚洲一区播放 | 欧美日韩一区二区在线 | 亚洲欧美激情国产综合久久久 | 国产午夜av片| www.婷婷亚洲基地 | 天天操天天怕 | 嫩草伊人 | 日韩一区二区视频 | 精品伦精品一区二区三区视频 | 9色网站 | 超碰在线人人 | 国产福利视频 | 天天草视频| 亚洲码欧美码一区二区三区 | 成人二区 | 99久久久久久 | 香蕉婷婷 | 国产欧美精品一区二区三区 | 国产精品久久国产精品 | 欧美日韩精品一区 | 狠狠操狠狠操 | 久久精品男人的天堂 | 草久久免费视频 | 亚洲网站在线播放 | 亚洲三区在线观看 | 国产精品久久久久永久免费观看 | 免费看国产精品视频 | 日韩精品免费 | 午夜精品视频 | 一区视频在线免费观看 | 国产一区二区三区四区五区加勒比 | 久久久噜噜噜久久中文字幕色伊伊 | 国产精品一二三区 | 九九爱这里只有精品 | 午夜av毛片 | 国产www成人 | 精品久久中文字幕 | 国产精品免费一区二区三区 | 91久久国产综合久久 | 久久久久久久久久久国产 |