如何使用laravel 5.1使用谁对记录进行更新的user_id填充modified_by? [英] How to populate the modified_by with the user_id of who made the update to the record using laravel 5.1?

查看:148
本文介绍了如何使用laravel 5.1使用谁对记录进行更新的user_id填充modified_by?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用Laravel 5.1时,我正在尝试创建一个观察器,该观察器将自动更新以下3列

When using Laravel 5.1, I am trying to create an Observer that will automatically update the following 3 columns

  1. created_by:在创建的记录中不再更新"时填充
  2. modified_by:每次修改记录时都会填充一个新值
  3. purged_by:软删除记录时填充一个值.

我知道Eloquent会自动更新日期时间戳(created_by,updated_at,remove_at),但是我需要更新进行更改的user_id.

I understand that Eloquent will automatically update the date time stamp (created_by, updated_at, removed_at) but I need to update the user_id that made the change.

建议我使用观察者和特质来处理此问题.这是我所做的

I was advised to use Observer and a trait to handle this. Here is what I have done

1)在app\Observers\ModelSignature.php中创建了一个名为"ModelSignature"的观察者类,这就是它的内容

1) created an observer class called "ModelSignature" located in app\Observers\ModelSignature.php and this is it's content

<?php 

namespace App\Observers;

class ModelSignature {

    protected $userID;

    public function __construct(){
        $this->userID =  Auth::id();
    }


    public function updating($model)
    {
        $model->modified_by = $this->userID;
    }


    public function creating($model)
    {
        $model->created_by = $this->userID;
    }


    public function removing($model)
    {
        $model->purged_by = $this->userID;
    }

}

然后我在app\Traits\RecordSignature.php中创建了一个名为"RecordSignature"的新特性,并包含以下代码

I then created a new Trait called "RecordSignature" located in app\Traits\RecordSignature.php and contains the following code

<?php

namespace App\Traits;

use app\Observers\ModelSignature;

trait RecordSignature
{

    public static function bootObservantTrait()
    {
        static::observe(new ModelSignature() );
    }
}

最后,在我位于"app \ Models \ Account.php"上的帐户"模型中,我像这样使用它

Finally, in my "Account" model located on "app\Models\Account.php" I use it like this

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use App\Models\industry;
use App\Traits\RecordSignature;
use App\Traits\TrimScalarValues;


class Account extends Model
{
    use RecordSignature, TrimScalarValues;
    /**
     * The database table used by the model.
     *
     * @var string
    */
    protected $table = 'accounts';

    protected $primaryKey = 'account_id';

    const CREATED_AT = 'created_on';

    const UPDATED_AT = 'modified_on';

    const REMOVED_AT = 'purged_on';


    /**
     * The attributes that are mass assignable.
     *
     * @var array
    */
    protected $fillable = ['client_id','account_name', 'company_code', 'legal_name', 'created_by','modified_by','instrucations'];

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
    */
    //protected $hidden = ['account_id', 'remember_token'];


    protected $guarded = ['account_id'];

    /**
     * Get the industry record associated with the account.
    */
    public function industry()
    {
        return $this->hasOne(industry, industry::primaryKey);
    }

    public function pk(){

        return $this->primaryKey;
    }

}

问题在于方法updating()removing()creating()没有按预期填充userId.似乎这些字段已被忽略或未触发方法!

The problem is that the methods updating(), removing() and creating() are not populating the userId as expected. It seems that the fields are being ignored or the methods are not being fired!

我在这里做什么错了?

推荐答案

您应该可以摆脱ModelSignatures类,并将其特征更改为以下内容:

You should be able to get rid of your ModelSignatures Class and change your trait to something like:

trait RecordSignature
{
    protected static function boot()
    {
        parent::boot();

        static::updating(function ($model) {

            $model->modified_by = \Auth::User()->id;
        });

        static::creating(function ($model) {

            $model->created_by = \Auth::User()->id;
        });
        //etc

    }

}


更新:

如今有许多软件包可以为您处理这类事情.我目前使用的很多内容是 Laravel审核,它不仅可以跟踪谁执行了任务,以及对行的任何更改.

There are many packages nowadays that will handle this sort of thing for you. One that I currently use quite a bit is Laravel Auditing which will not only keep track of who performed a task but also any changes to row as well.

这篇关于如何使用laravel 5.1使用谁对记录进行更新的user_id填充modified_by?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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