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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18398489/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 08:19:23  来源:igfitidea点击:

Laravel: performing some task on every insert/update when using Query Builder or Eloquent ORM

phplaravellaravel-4eloquentquery-builder

提问by rgvcorley

The Problem

问题

I would like to automatically add created_byand modified_byfields 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.

我想在 Laravel 4 中为每个插入/更新数据库表自动添加created_bymodified_by字段,无论我使用的是 Eloquent 还是 Query Builder。但是,并非我的所有表都有这些字段,因此任何解决方案都必须在添加之前检查这些列是否存在。

Attempted Solution

尝试的解决方案

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

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

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

这很好,只是如果我使用查询生成器执行插入,则会绕过。查看Model该类,似乎数据库操作实际上是使用查询构建器完成的。

I have had a look at the Illuminate\Database\Query\Buildermodel and it looks like I could probably write overwrite methods for insert()and update().

我看过Illuminate\Database\Query\Builder模型,看起来我可以为insert()和编写覆盖方法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?

这是为每次插入/更新执行某些任务的明智方法,还是我以后会遇到麻烦?

回答by Mohamed Azher

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

补充上面的答案。你可以做这样的事情。

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

在应用程序/模型中创建一个名为 BaseModel.php 扩展 \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;
    });
  }

}

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

然后在您的各个模型类中,您需要扩展 BaseModel 而不是 \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();
    }

}

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

现在,无论何时保存或更新模型, created_by 和 updated_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.

注意:这仅在通过 Eloquent 完成保存或更新时有效。对于查询构建器,您可以使用通用方法来获取和附加 created_by 和 update_by 列更新。

回答by Atrakeur

You must never override the save method to override and add your functionnality.

您绝不能覆盖 save 方法来覆盖和添加您的功能。

You have to use the Model Events functionnality that eloquent provides to do that instead.

您必须使用 eloquent 提供的模型事件功能来代替。

To put it simply, you have to define a saving event for you model to override/set/check data that the model is going to save.

简而言之,您必须为模型定义一个保存事件来覆盖/设置/检查模型将要保存的数据。

A simple example to put in a User model class:

放入 User 模型类的简单示例:

//Executed when loading model
public static function boot()
{
     parent::boot();

     User::creating(function($user){
         $user->value1 = $user->value2 +1;
     });
}

More information: http://four.laravel.com/docs/eloquent#model-events

更多信息:http: //four.laravel.com/docs/eloquent#model-events

回答by Ben

If you want to use Query Builder, and Eloquent the only way around this without extending the Core Components (which I don't deem necessary), you can just use the Event System.

如果您想使用 Query Builder,并且 Eloquent 是解决此问题的唯一方法,而无需扩展核心组件(我认为没有必要),您可以只使用Event System

Link: http://laravel.com/docs/events

链接:http: //laravel.com/docs/events

So you'd use an event such as user.custom.save, then create a function for use with the query builder which at the end would trigger this event, same as with Eloquent.

因此,您将使用诸如 之类的事件user.custom.save,然后创建一个与查询构建器一起使用的函数,该函数最终会触发此事件,与 Eloquent 相同。

Example:

例子:

class User extends Eloquent
{
    public function save()
    {
        Event::fire('user.custom.save', array($this));
        parent::save();
    }
}

回答by ruwan800

In Laravel 5.3 if you like to call a single method on each save/updatefrom a single point without making any additional changes in each of the extended Models, you can have a custom listener for eloquent events. As documetationsays it can be done only for per Model. But creating a custom listener allow to access any event in any Model.

在 Laravel 5.3 中,如果您想从单个点调用每个保存/更新单个方法,而无需在每个扩展模型中进行任何额外更改,您可以为 eloquent 事件设置一个自定义侦听器。正如文档所说,它只能针对每个模型完成。但是创建自定义侦听器允许访问任何模型中的任何事件。

Just add a listener to boot()method in EventServiceProviderlike below and modify accordingly.

只需boot()EventServiceProvider如下所示的方法中添加一个侦听器并进行相应修改。

Event::listen(['eloquent.saving: *', 'eloquent.creating: *'], function(){
        //your method content
        //returning false will cancel saving the model
 });

Please note that wildcard used to match any model. See documentationfor more on events.

请注意通配符用于匹配任何模型。有关事件的更多信息,请参阅文档

回答by fico7489

You can use venturecraft revisionable package because in a table from this package all info that you need is already stored, you just need this package to get them in an elegant way : https://github.com/fico7489/laravel-revisionable-upgrade

您可以使用 Venturecraft 可修订包,因为在此包的表中,您需要的所有信息都已存储,您只需要此包即可以优雅的方式获取它们:https: //github.com/fico7489/laravel-revisionable-upgrade