Jav*_*sta 3 laravel laravel-auditing
我想在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']);
});
Run Code Online (Sandbox Code Playgroud)
我已经修改了这个解析函数,试图在新字段中存储一些内容,但事实并非如此。
\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
];
Run Code Online (Sandbox Code Playgroud)
它应该存储'Custom Value'在'custom'现场,但它只是不存储任何东西。我可能正在查看错误的函数,或者这可能不是审核新自定义数据的方式。
小智 5
我在用着laravel/framework: 8.0&owen-it/laravel-auditing: ^10.0.0。
transformAudit()通过将以下函数添加到任何模型来覆盖审核方法,implements Auditable以扩展$data数组(源)。
在文件顶部:
use Illuminate\Support\Arr;
Run Code Online (Sandbox Code Playgroud)
在您的模型定义中:
public function transformAudit(array $data): array
{
Arr::set($data, 'custom', 'Custom Value');
return $data;
}
Run Code Online (Sandbox Code Playgroud)
一般来说,由于/vendor/各种原因您不想编辑文件。
| 归档时间: |
|
| 查看次数: |
3719 次 |
| 最近记录: |