PHP code example of vimatech / laravel-audit-log

1. Go to this page and download the library: Download vimatech/laravel-audit-log 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/ */

    

vimatech / laravel-audit-log example snippets


->withMiddleware(function (Middleware $middleware): void {
    $middleware->web(append: [\Vimatech\AuditLog\Http\Middleware\SetAuditContext::class]);
    $middleware->api(append: [\Vimatech\AuditLog\Http\Middleware\SetAuditContext::class]);
    $middleware->appendToGroup('admin', \Vimatech\AuditLog\Http\Middleware\SetAuditContext::class);
})

use Vimatech\AuditLog\Facades\Audit;

Audit::action('quota.overridden')
    ->on($workspace)                       // subject
    ->from(['properties' => 5])
    ->to(['properties' => 12])
    ->because('Pilot customer, ticket SUP-42')
    ->record();

// or the plain call
Audit::record('workspace.suspended', $workspace, reason: 'Chargeback');

app(AuditContext::class)->inTenant($workspace)->actingAs($admin, 'admin')->because('Nightly reconciliation');

final class Lease extends Model implements ProvidesAuditTenant
{
    public function auditTenant(): ?Model
    {
        return $this->workspace;
    }
}

Audit::action('backup.completed')->withoutTenant()->record();
Audit::action('lease.exported')->on($lease)->withoutTenant()->record();  // not the lease's tenant

use Vimatech\AuditLog\Concerns\Auditable;

final class Lease extends Model implements ProvidesAuditTenant
{
    use Auditable;

    protected $hidden = ['tenant_phone'];          // never logged
    protected array $auditExclude = ['workspace_id']; // never logged either
}

AuditEntry::query()
    ->forTenant($workspace)
    ->forSubject($lease)
    ->byActor($user)
    ->forAction(['lease.updated', 'lease.deleted'])
    ->between($from, $until)
    ->latestFirst()
    ->paginate();

AuditEntry::query()->forTenant($workspace)->latestFirst()->paginate();
AuditEntry::query()->forCurrentTenant()->latestFirst()->paginate();   // the tenant on AuditContext

use Vimatech\AuditLog\Contracts\RetentionPolicy;
use Vimatech\AuditLog\Retention\KeepFor;

$this->app->bind(RetentionPolicy::class, fn () => KeepFor::years(2));

public function cutoff(): ?DateTimeInterface;   // null keeps everything

use Vimatech\AuditLog\Events\AuditEntryRecorded;

Event::listen(function (AuditEntryRecorded $event): void {
    // $event->entry
});

// config/audit-log.php

return [
    'models' => [
        'entry' => \Vimatech\AuditLog\Models\AuditEntry::class,
    ],

    'tables' => [
        'entries' => 'audit_log_entries',
    ],

    // Database connection holding the entries table. Null means the default
    // connection. The migration and the append-only triggers follow this value.
    'connection' => null,

    // Refuse to record an entry when no tenant could be resolved, from the argument,
    // from the subject's ProvidesAuditTenant, or from AuditContext. Off by default:
    // a null tenant is a faithful record of an action that had none, and not every
    // application is multi-tenant. Turn it on to catch the call site that forgot,
    // and mark the genuinely tenant-less ones with ->withoutTenant().
    ' every entry. A policy carrying a value, such as KeepFor::days(730), cannot be
    // named here and still survive `config:cache`: bind RetentionPolicy in a service
    // provider instead.
    'retention' => \Vimatech\AuditLog\Retention\KeepForever::class,
];
bash
php artisan vendor:publish --tag=audit-log-config