Laravel:在使用查询生成器或 Eloquent ORM 时在每次插入/更新时执行一些任务

时间:2023-03-04
本文介绍了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 时在每次插入/更新时执行一些任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

相关文章