无效的用户解析程序,可调用或UserResolver FQCN预期错误[Laravel Auditing 4.1]

时间:2017-11-24 18:36:23

标签: laravel laravel-5.4 laravel-auditing

我正在使用Laravel 5.4和Laravel Auditing 4.1。我想在Variant模型上记录价格变化,我安装了Auditing 4.1软件包。看我的代码,

配置/ audit.php

'implementation' => OwenIt\Auditing\Models\Audit::class,

'user' => [
    'primary_key' => 'id',
    'foreign_key' => 'variant_id',
    'model'       => App\Variant::class,
    'resolver'    => App\Variant::class,
],

应用/型号/ Variant.php

use OwenIt\Auditing\Auditable;
use OwenIt\Auditing\Contracts\Auditable as AuditableContract;

class Variant extends BaseModel implements AuditableContract{

   use Auditable;
 --------Code here----------------
}

当我尝试将数据插入数据库时​​使用此代码,将出现此错误,

 UnexpectedValueException
 Invalid User resolver, callable or UserResolver FQCN expected

如何解决此错误并在audits表上记录我的更改?

1 个答案:

答案 0 :(得分:0)

这是因为您在App\Variant::class中配置的用于处理用户解析的课程(config/audit.php)未实施OwenIt\Auditing\Contracts\UserResolver合同。

因此,如果您真的想使用App\Variant::class作为用户解析程序类,那么它应该是这样的:

<?php
namespace App;

use OwenIt\Auditing\Auditable;
use OwenIt\Auditing\Contracts\Auditable as AuditableContract;

class Variant extends BaseModel implements AuditableContract, UserResolver
{
    use Auditable;

    public static function resolveId()
    {
        return Auth::check() ? Auth::user()->getAuthIdentifier() : null;
    }

    // More code here
}

如有疑问,请查看General Configuration文档。