【问题标题】:How to audit new custom data in Laravel Auditing's table?如何审计 Laravel 审计表中的新自定义数据?
【发布时间】:2020-01-05 09:32:24
【问题描述】:

我想在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']);
        });

我已经修改了这个解析函数,试图在新字段中存储一些东西,但它没有。

\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',但它只是不存储任何内容。我可能正在查看错误的函数,或者这可能不是他审核新自定义数据的方式。

【问题讨论】:

    标签: laravel laravel-auditing


    【解决方案1】:

    我正在使用laravel/framework: 8.0 & owen-it/laravel-auditing: ^10.0.0

    通过将以下函数添加到 implements Auditable 的任何模型来覆盖 transformAudit() 审计方法,以扩展 $data 数组 (source)。

    文件顶部:

    use Illuminate\Support\Arr;
    

    在您的模型定义中:

    public function transformAudit(array $data): array
    {
        Arr::set($data, 'custom',  'Custom Value');
    
        return $data;
    }
    

    一般来说,出于各种原因,您不想编辑/vendor/ 文件。

    【讨论】:

      猜你喜欢
      • 2016-06-02
      • 2015-02-15
      • 2018-11-23
      • 2021-01-26
      • 2010-12-14
      • 2021-05-20
      • 2011-03-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多