PHP code example of tapp / filament-auditing

1. Go to this page and download the library: Download tapp/filament-auditing library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

tapp / filament-auditing example snippets




return [

    'audits_sort' => [
        'column' => 'created_at',
        'direction' => 'desc',
    ],

    'is_lazy' => true,
    
    'audits_extend' => [
        // 'url' => [
        //     'class' => \Filament\Tables\Columns\TextColumn::class,
        //     'methods' => [
        //         'sortable',
        //         'searchable' => true,
        //         'default' => 'N/A'
        //     ]
        // ],
    ]

    'custom_audits_view' => false,

    'custom_view_parameters' => [
    ],

    'mapping' => [
    ],
];

use Tapp\FilamentAuditing\RelationManagers\AuditsRelationManager;

public static function getRelations(): array
{
    return [
        // ...
        AuditsRelationManager::class,
    ];
}



return [

    'audits_extend' => [
       'url' => [
           'class' => \Filament\Tables\Columns\TextColumn::class, // 



namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use OwenIt\Auditing\Contracts\Audit;
use OwenIt\Auditing\Contracts\Auditable;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Support\Arr;
use Illuminate\Support\HtmlString;

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

    // ...

    public function formatAuditFieldsForPresentation($field, Audit $record)
    {
        $fields = Arr::wrap($record->{$field});

        $formattedResult = '<ul>';

        foreach ($fields as $key => $value) {
            $formattedResult .= '<li>';
            $formattedResult .= match ($key) {
                'user_id' => '<strong>User</strong>: '.User::find($record->{$field}['user_id'])?->name.'<br />',
                'title' => '<strong>Title</strong>: '.(string) str($record->{$field}['title'])->title().'<br />',
                'order' => '<strong>Order</strong>: '.$record->{$field}['order'].'<br />',
                'content' => '<strong>Content</strong>: '.$record->{$field}['content'].'<br />',
                default => ' - ',
            };
            $formattedResult .= '</li>';
        }

        $formattedResult .= '</ul>';

        return new HtmlString($formattedResult);
    }

    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class);
    }
}

'custom_audits_view' => true,

@php
$type = (string) str(class_basename($owner))->lower();
@endphp

@if(isset($records))
    <x-filament-tables::table>
        <x-slot name="header">
            @foreach($headers as $header)
            <x-filament-tables::header-cell>
                {{$header}}
            </x-filament-tables::header-cell>
            @endforeach
        </x-slot>
        @foreach($records as $audit)
            <x-filament-tables::row>
                @foreach ($audit->getModified() as $attribute => $modified)
                    <x-filament-tables::cell>
                        @lang($type.'.metadata', $audit->getMetadata())
                        <br />
                        @php
                            $current = $type.'.'.$audit->event.'.modified.'.$attribute;

                            $modified['new'] = $owner->formatFieldForPresentation($attribute, $modified['new']);

                            if (isset($modified['old'])) {
                                $modified['old'] = $owner->formatFieldForPresentation($attribute, $modified['old']);
                            }
                        @endphp

                        @lang($current, $modified)
                    </x-filament-tables::cell>
                @endforeach
            </x-filament-tables::row>
        @endforeach
    </x-filament-tables::table>
@else
    <div class="flex items-center justify-center h-32 text-gray-500 dark:text-gray-400">
        @lang($type.'.unavailable_audits')
    </div>
@endif

'custom_view_parameters' => [
    'headers' => [
        'Audit',
    ],
],

public function formatFieldForPresentation($field, $value)
{
    return match($field) {
        'user_id' => $value ? optional(User::find($value))->name : $value,
        default => $value,
    };
}



return [
    'unavailable_audits' => 'No article audits available',

    'metadata' => 'On :audit_created_at, :user_name [:audit_ip_address] :audit_event this record via :audit_url',

    'updated' => [
        'modified' => [
            'order' => 'The Order has been modified from <strong>:old</strong> to <strong>:new</strong>',
            'title' => 'The Title has been modified from <strong>:old</strong> to <strong>:new</strong>',
            'content' => 'The Content has been modified from <strong>:old</strong> to <strong>:new</strong>',
            'user_id' => 'The User has been modified from <strong>:old</strong> to <strong>:new</strong>',
        ],
    ],
];

protected $listeners = [
    'auditRestored',
];

public function auditRestored()
{
    // your code
}

protected function afterSave(): void
{
    $this->dispatch('updateAuditsRelationManager');
}
bash
php artisan vendor:publish --tag="filament-auditing-views"
bash
php artisan vendor:publish --tag="filament-auditing-translations"
bash
php artisan vendor:publish --tag="filament-auditing-config"
bash
php artisan optimize