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

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

      1. <legend id='n5FqE'><style id='n5FqE'><dir id='n5FqE'><q id='n5FqE'></q></dir></style></legend>

        <tfoot id='n5FqE'></tfoot>

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

        Laravel:在使用查詢生成器或 Eloquent ORM 時在每次插

        Laravel: performing some task on every insert/update when using Query Builder or Eloquent ORM(Laravel:在使用查詢生成器或 Eloquent ORM 時在每次插入/更新時執行一些任務)
        <legend id='EVEYH'><style id='EVEYH'><dir id='EVEYH'><q id='EVEYH'></q></dir></style></legend>

          <tbody id='EVEYH'></tbody>

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

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

          1. <tfoot id='EVEYH'></tfoot>
                  本文介紹了Laravel:在使用查詢生成器或 Eloquent ORM 時在每次插入/更新時執行一些任務的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  我想在 Laravel 4 中每次插入/更新數據庫表時自動添加 created_bymodified_by 字段,無論我使用的是 Eloquent 還是 Query Builder.但是,并非我所有的表都有這些字段,因此任何解決方案都必須在添加之前檢查這些列是否存在.

                  I would like to automatically add created_by and modified_by fields to every insert/update to a database table in Laravel 4, regardless of whether I am using Eloquent or Query Builder. However, not all my tables have these fields so any solution will have to check these columns exist before adding.

                  我已經擴展了 IlluminateDatabaseEloquentModel 類并編寫了一個覆蓋方法 save() 以便為每個保存的記錄添加一些額外的元數據字段.

                  I have extended the IlluminateDatabaseEloquentModel class and written an overwrite method save() in order to add some additional meta data fields for every record that is saved.

                  這很好,只是如果我使用查詢生成器執行插入,則會被繞過.查看 Model 類,似乎數據庫操作實際上是使用查詢構建器完成的.

                  This is fine except that if I perform an insert using the Query Builder then this is bypassed. Looking at the Model class it appears that the database operations are actually done using the query builder.

                  我看過IlluminateDatabaseQueryBuilder 模型,看起來我可以為 insert()update() 編寫覆蓋方法.

                  I have had a look at the IlluminateDatabaseQueryBuilder model and it looks like I could probably write overwrite methods for insert() and update().

                  這是為每次插入/更新執行某些任務的明智方法,還是我以后會遇到麻煩?

                  Is this a sensible way to go about performing some task for every insert/update or will I run into trouble later down the line?

                  推薦答案

                  添加到上述答案中.你可以做這樣的事情.

                  Adding to the above answers. You could do something like this.

                  在應用程序/模型中創建一個名為 BaseModel.php 擴展 Eloquent 的類

                  Create a class in app/models called BaseModel.php extending Eloquent

                  class BaseModel extends Eloquent{
                  
                  public static function boot()
                  {
                      parent::boot();
                  
                      static::creating(function($model)
                      {
                          //change to Auth::user() if you are using the default auth provider
                          $user = Confide::user();
                          $model->created_by = $user->id;
                          $model->updated_by = $user->id;
                      });
                  
                      static::updating(function($model)
                      {
                          //change to Auth::user() if you are using the default auth provider
                          $user = Confide::user();
                          $model->updated_by = $user->id;
                      });
                    }
                  
                  }
                  

                  然后在您的各個模型類中,您需要擴展 BaseModel 而不是 Eloquent

                  Then in your individual model classes you need to extent the BaseModel instead of Eloquent

                  class Product extends BaseModel {
                  
                      protected $table = 'product';
                  
                      //Booting the base model to add created_by and updated_by to all tables
                      public static function boot()
                      {
                          parent::boot();
                      }
                  
                  }
                  

                  現在,無論何時保存或更新模型,created_by 和 updated_by 字段都會自動更新.

                  Now any time you save or update a model, the created_by and updated_by fields would be updated automatically.

                  注意:這僅在通過 Eloquent 完成保存或更新時有效.對于查詢構建器,您可以使用通用方法來獲取和附加 created_by 和 update_by 列更新.

                  Note: This would only work when save or update is done through Eloquent. For query builder, you could have a common method to fetch and append the created_by and update_by column updates.

                  這篇關于Laravel:在使用查詢生成器或 Eloquent ORM 時在每次插入/更新時執行一些任務的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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='z8vdY'></small><noframes id='z8vdY'>

                    <bdo id='z8vdY'></bdo><ul id='z8vdY'></ul>

                        <tbody id='z8vdY'></tbody>
                        <legend id='z8vdY'><style id='z8vdY'><dir id='z8vdY'><q id='z8vdY'></q></dir></style></legend>
                        <i id='z8vdY'><tr id='z8vdY'><dt id='z8vdY'><q id='z8vdY'><span id='z8vdY'><b id='z8vdY'><form id='z8vdY'><ins id='z8vdY'></ins><ul id='z8vdY'></ul><sub id='z8vdY'></sub></form><legend id='z8vdY'></legend><bdo id='z8vdY'><pre id='z8vdY'><center id='z8vdY'></center></pre></bdo></b><th id='z8vdY'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='z8vdY'><tfoot id='z8vdY'></tfoot><dl id='z8vdY'><fieldset id='z8vdY'></fieldset></dl></div>

                          • <tfoot id='z8vdY'></tfoot>
                            主站蜘蛛池模板: 精品日韩一区二区三区av动图 | 中文字幕视频一区 | 91网站视频在线观看 | 日韩av成人 | 日韩精品亚洲专区在线观看 | 成人精品在线视频 | 欧美精品网 | 亚洲性视频网站 | 天天射天天操天天干 | 亚洲视频在线看 | 国产精品久久国产精品99 | 国产在线1 | 久久久久久久夜 | 免费黄色片视频 | 日韩一区二区三区在线观看 | 一区二区三区亚洲视频 | 操操网站 | 日韩成人在线播放 | 国产一区二区三区四区五区加勒比 | 国产精品一区二区久久 | 中文字幕日韩欧美一区二区三区 | 国产精品视频网 | 精品欧美一区二区三区精品久久 | 伊人久久综合 | 欧美乱淫视频 | 国产精品日韩高清伦字幕搜索 | 国产91综合一区在线观看 | 日韩精品一区二区三区中文字幕 | 一本一道久久a久久精品蜜桃 | 午夜精| 一区二区三区视频 | 久久偷人 | 欧美日韩精品久久久免费观看 | 国产激情福利 | 午夜看片| 久久久免费 | 中文字幕1区 | 欧美男人亚洲天堂 | 国产精品一区二 | 国产精品高清在线 | 搞av.com|