2

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

配置/审计.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表格上记录我的更改?

4

1 回答 1

1

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
}

如有疑问,请查看通用配置文档。

于 2018-02-22T22:06:20.970 回答