<tfoot id='qUAOG'></tfoot><legend id='qUAOG'><style id='qUAOG'><dir id='qUAOG'><q id='qUAOG'></q></dir></style></legend>

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

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

        Laravel:使用 Query Builder 或 Eloquent ORM 时在每次插入/更新时执行一些任务

        Laravel: performing some task on every insert/update when using Query Builder or Eloquent ORM(Laravel:使用 Query Builder 或 Eloquent ORM 时在每次插入/更新时执行一些任务)
              <tbody id='UC3zN'></tbody>
                <bdo id='UC3zN'></bdo><ul id='UC3zN'></ul>

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

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

                1. <tfoot id='UC3zN'></tfoot>
                2. <legend id='UC3zN'><style id='UC3zN'><dir id='UC3zN'><q id='UC3zN'></q></dir></style></legend>
                3. 本文介绍了Laravel:使用 Query Builder 或 Eloquent ORM 时在每次插入/更新时执行一些任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  无论我使用的是 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.

                  在 app/models 中创建一个名为 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:使用 Query Builder 或 Eloquent ORM 时在每次插入/更新时执行一些任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
                  PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
                  mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
                  Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
                  Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
                  Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)

                    <tbody id='iGLWH'></tbody>

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

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

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