我希望将新数据存储在Laravel Auditing
日志中,并在Laravel Auditing
使用的自动日志中注册新字段。我正在尝试在Laravel Auditing
的表中添加一个新字段,以便在审计完成时可以记录自定义数据。我正在使用一个几乎全新的Laravel 5.8.31
安装。
我正在将新数据添加到audits
表的存储中。我正在修改迁移文件2019_08_26_083436_create_audits_table.php
中的表字段,以添加一个新的自定义字段。
Schema::create('audits', function (Blueprint $table) {
$table->increments('id');
$table->string('user_type')->nullable();
$table->unsignedBigInteger('user_id')->nullable();
$table->string('event');
$table->morphs('auditable');
$table->text('old_values')->nullable();
$table->text('new_values')->nullable();
$table->text('url')->nullable();
$table->ipAddress('ip_address')->nullable();
$table->string('user_agent')->nullable();
$table->string('tags')->nullable();
$table->timestamps();
$table->text('custom')->nullable(); <--- Like this one
$table->index(['user_id', 'user_type']);
});
我已经修改了这个resolve函数,试图在新的字段中存储一些内容,但它没有。
\My-project\vendor\owen-it\laravel-auditing\src\Audit.php
public function resolveData(): array
{
$morphPrefix = Config::get('audit.user.morph_prefix', 'user');
// Metadata
$this->data = [
'audit_id' => $this->id,
'audit_event' => $this->event,
'audit_url' => $this->url,
'audit_ip_address' => $this->ip_address,
'audit_user_agent' => $this->user_agent,
'audit_tags' => $this->tags,
'audit_created_at' => $this->serializeDate($this->created_at),
'audit_updated_at' => $this->serializeDate($this->updated_at),
'user_id' => $this->getAttribute($morphPrefix.'_id'),
'user_type' => $this->getAttribute($morphPrefix.'_type'),
'custom' => 'Custom Value', <--- Some new value
];
它应该在'custom'
字段中存储'Custom Value'
,但是它没有存储任何内容。我可能看错了函数,或者这可能不是审计新的自定义数据的方法。
发布于 2021-04-28 17:33:17
我使用的是laravel/framework: 8.0
& owen-it/laravel-auditing: ^10.0.0
。
通过将以下函数添加到任何implements Auditable
的模型以扩展$data
数组(source)来覆盖审计的transformAudit()
方法。
在文件的顶部:
use Illuminate\Support\Arr;
在您的模型定义中:
public function transformAudit(array $data): array
{
Arr::set($data, 'custom', 'Custom Value');
return $data;
}
一般来说,由于各种原因,您不希望编辑/vendor/
文件。
https://stackoverflow.com/questions/57755360
复制相似问题