如何使用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?

当使用Laravel 5.1时,我试图创建一个观察者,它将自动更新以下3列

  1. created_by:当创建的记录"never update this again"时填充
  2. modified_by:每次记录被修改时填充一个新值
  3. purged_by:在软删除记录时填充一个值

我知道Eloquent会自动更新日期时间戳(created_by, updated_at, removed_at),但我需要更新做出更改的user_id。

我被建议使用Observer和trait来处理这个问题。以下是我所做的

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

<?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"的新Trait,并包含以下代码

<?php
namespace App'Traits;
use app'Observers'ModelSignature;
trait RecordSignature
{
    public static function bootObservantTrait()
    {
        static::observe(new ModelSignature() );
    }
}

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

<?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。似乎字段被忽略了,或者方法没有被触发!

我在这里做错了什么?

您应该能够摆脱您的ModelSignatures类并将您的trait更改为如下内容:

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 Auditing,它不仅可以跟踪谁执行了一个任务,还可以跟踪行的任何更改。

我建议您在此升级中使用venturecraft可修订包https://github.com/fico7489/laravel-revisionable-upgrade

一切都将开箱工作,你只需要使用2个特征,你可以做更多的信息,你可以得到谁编辑模型,你可以得到谁编辑特定列,甚至特定值的信息等等。

第一步:制作trait

namespace App'Traits;
use App'Observer'ModelObserver;
trait ObservantTrait
{
    public static function bootObservantTrait()
    {
        static::observe(ModelObserver::class);
    }
}

步骤2:创建观察者

<?php
namespace App'Observer;
use Illuminate'Auth'AuthManager;
use Illuminate'Contracts'Config'Repository;
use Request;
class ModelObserver
{
    public $userID;
    protected $auth;
    protected $login_user;
    protected $ip;
    protected $user_agent;
    public function __construct(AuthManager $auth, Repository $config)
    {
        $this->auth = $auth;
        $this->login_user = $auth->guard($this->auth->getDefaultDriver())->user();
        if (isset($this->login_user->id)) {
            $this->userID = $this->login_user->id;
            $this->ip = Request::ip();
            $this->user_agent = Request::header('user-agent');
        }
    }

    public function updating($model)
    {
        $model->updated_by = $this->userID;
        $model->ip = Request::ip();
        $model->user_agent = Request::header('user-agent');
    }
    public function creating($model)
    {
        $model->created_by = $this->userID;
        $model->ip = Request::ip();
        $model->user_agent = Request::header('user-agent');
    }
    public function deleted($model)
    {
        $model->deleted_by = $this->userID;
        $model->ip = Request::ip();
        $model->user_agent = Request::header('user-agent');
        $model->save();
    }
}

步骤3:user trait in model

use ObservantTrait;