Laravel:在使用Query Builder或Eloquent ORM时对每次插入/更新执行一些任务 [英] Laravel: performing some task on every insert/update when using Query Builder or Eloquent ORM

查看:538
本文介绍了Laravel:在使用Query Builder或Eloquent ORM时对每次插入/更新执行一些任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想自动添加 created_by modified_by 字段到Laravel 4中的数据库表的每个插入/更新,无论我是使用Eloquent还是查询生成器。但是,并不是所有的表都有这些字段,所以任何解决方案都必须在添加之前检查这些列。

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.

我已经扩展了 Illuminate\ Database\Eloquent\Model 类,并写了一个覆盖方法 save(),以便添加一些额外的元数据字段每个保存的记录。

I have extended the Illuminate\Database\Eloquent\Model 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.

我已经看看 Illuminate\Database\Query \Builder 模型,看起来我可能会为 insert()更新编写覆盖方法()

这是一个合理的方式来执行每个插入/更新的一些任务,或者我会遇到麻烦, line?

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆