PHP code example of graymatter / laravel-audit-chain

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

    

graymatter / laravel-audit-chain example snippets


use GrayMatter\AuditChain\Concerns\HasActivityLog;
use GrayMatter\AuditChain\Contracts\Auditable;

class Post extends Model implements Auditable
{
    use HasActivityLog;
}

use GrayMatter\AuditChain\Concerns\HasAuditTrail;
use GrayMatter\AuditChain\Contracts\Auditable;

class Licence extends Model implements Auditable
{
    use HasAuditTrail;
}

// Simple event
$order->audit('shipped');

// Event with old/new values
$order->audit('status_changed',
    oldValues: ['status' => 'pending'],
    newValues: ['status' => 'shipped'],
);

use GrayMatter\AuditChain\Facades\AuditChain;

AuditChain::batch(function () {
    $order->audit('shipped');
    $order->update(['status' => 'shipped']);
    $inventory->update(['quantity' => $inventory->quantity - 1]);
});
// All 3 audit logs share the same batch_uuid

AuditChain::context(['source' => 'csv_import', 'file' => 'users.csv']);

// All audit logs created after this will 

AuditChain::withoutAudit(function () {
    // No audit logs created during this callback
    User::factory()->count(1000)->create();
});

use GrayMatter\AuditChain\Attributes\PersonalData;

class User extends Model implements Auditable
{
    use HasAuditTrail;

    #[PersonalData(description: 'User email address')]
    public string $email;

    #[PersonalData]
    public string $name;
}

class User extends Model implements Auditable
{
    use HasAuditTrail;

    protected array $personalData = ['email', 'name'];
}

class User extends Model implements Auditable
{
    use HasAuditTrail;

    // Only audit these fields
    protected array $auditInclude = ['name', 'email', 'role'];

    // Or exclude specific fields
    protected array $auditExclude = ['last_login_at'];
}

$user->auditLogs; // MorphMany relation
$user->auditLogs()->where('event', 'updated')->get();

$data = $user->exportPersonalData();
// ['email' => '[email protected]', 'name' => 'John Doe']

$export = $user->exportFullSubjectData();
// [
//     'personal_data' => ['email' => '[email protected]', 'name' => 'John Doe'],
//     'audit_trail' => [
//         ['id' => '...', 'event' => 'created', 'old_values' => [], 'new_values' => [...], ...],
//         ['id' => '...', 'event' => 'updated', ...],
//     ],
// ]

$user->anonymize();
// email => '[ANONYMIZED]-42', name => '[ANONYMIZED]-42'

// config/audit-chain.php
'events' => [
    'log_reads' => true, // WARNING: very verbose
],

// routes/console.php (Laravel 11+)
Schedule::command('audit:verify --notify')->hourly();
Schedule::command('audit:prune --days=90')->daily();

// config/audit-chain.php
'notifications' => [
    'channels' => ['mail', 'webhook'],
    'mail_to' => [env('AUDIT_ALERT_EMAIL', '')],
    'webhooks' => [
        env('AUDIT_ALERT_WEBHOOK_1'),
        // Add more webhook URLs as needed
    ],
],

use GrayMatter\AuditChain\Services\AuditChainService;

$result = app(AuditChainService::class)->verifyChain();
// ['valid' => true, 'checked' => 150, 'errors' => []]

// config/audit-chain.php

return [
    'connection' => null,
    'table' => 'audit_logs',
    'drivers' => ['database'],
    'chain_seed' => env('AUDIT_CHAIN_SEED', 'genesis'),
    'queue' => [
        'enabled' => true,
        'connection' => null,
        'queue' => 'default',
    ],
    'events' => [
        'log_reads' => false,
    ],
    'anonymization' => [
        'replacement' => '[ANONYMIZED]',
    ],
    'retention' => [
        'days' => 90,
    ],
    'notifications' => [
        'channels' => ['mail'],
        'mail_to' => [env('AUDIT_ALERT_EMAIL', '')],
        'webhooks' => [],
    ],
];
bash
php artisan vendor:publish --tag="audit-chain-config"
php artisan vendor:publish --tag="audit-chain-migrations"
php artisan migrate
bash
# Delete logs older than 90 days (default)
php artisan audit:prune

# Custom retention period
php artisan audit:prune --days=365

# Prune only specific model type
php artisan audit:prune --type="App\Models\User"