How to audit new custom data in Laravel Auditing's table?

I want to store new data in the Laravel Auditing logs and register new field in the automatic logs used by Laravel Auditing. I'm trying to add a new field in the Laravel Auditing's table so I can log custom data when the auditing is done. I am using a practically fresh Laravel 5.8.31 instalation.

I am adding new data to storage at the audits table. I am modifying the table fields at the migration file 2019_08_26_083436_create_audits_table.php to add a new custom field.

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

I've modified this resolve function trying to store something in the new field but it doesn't.

\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
        ];

It should've store 'Custom Value' at 'custom' field but it just doesn't store anything. I am maybe looking at the wrong function or this is maybe not he way to audit new custom data.

Upvotes: 3

Views: 4488

Answers (2)

Nathan
Nathan

Reputation: 109

Use Tags

<?php
  namespace App\Models;
  use Illuminate\Database\Eloquent\Model;
  use OwenIt\Auditing\Contracts\Auditable;

  class Article extends Model implements Auditable
  {
      use \OwenIt\Auditing\Auditable;

      /**
       * {@inheritdoc}
       */
      public function generateTags(): array
      {
          return [
              $this->editor->name,
              $this->reporter->name,
              $this->designer->name,
              $this->photographer->name,
          ];
      }

      // ...
  }

Upvotes: 0

Marc Bosse
Marc Bosse

Reputation: 401

I'm using laravel/framework: 8.0 & owen-it/laravel-auditing: ^10.0.0.

Override the transformAudit() method of Auditing by adding the following function to any Model that implements Auditable in order to extend the $data array (source).

At top of file:

use Illuminate\Support\Arr;

Within your Model definition:

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

    return $data;
}

As a general rule, you don't want to edit /vendor/ files for various reasons.

Upvotes: 5

Related Questions