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

  • <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 id='EVEYH'><tfoot id='EVEYH'></tfoot><dl id='EVEYH'><fieldset id='EVEYH'></fieldset></dl></div>

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

        Laravel:在使用查询生成器或 Eloquent ORM 时在每次插

        时间:2023-09-24
        • <small id='n5FqE'></small><noframes id='n5FqE'>

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

                  <tbody id='n5FqE'></tbody>
                <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 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 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模板网!

                  上一篇:Laravel Eloquent::Find() 使用现有 ID 返回 NULL 下一篇:laravel 雄辩的一对多关系返回 null

                  相关文章

                  最新文章

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

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

                  1. <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 id='z8vdY'><tfoot id='z8vdY'></tfoot><dl id='z8vdY'><fieldset id='z8vdY'></fieldset></dl></div>

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