PHP code example of darken10 / auditra

1. Go to this page and download the library: Download darken10/auditra 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/ */

    

darken10 / auditra example snippets




namespace App\Models;

use Darken10\Auditra\Traits\Auditable;
use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    use Auditable;

    // Optionnel : spécifier les attributs à auditer
    protected $auditableAttributes = ['name', 'email', 'status'];
    
    // Optionnel : exclure certains attributs
    protected $auditExclude = ['password', 'remember_token'];
    
    // Optionnel : désactiver l'audit pour ce modèle
    protected $auditEnabled = true;
}

use Darken10\Auditra\Facades\Auditra;

// Audit d'une action personnalisée
Auditra::manual('login_attempt', $user, [
    'metadata' => ['ip' => request()->ip()]
]);

// Audit sans modèle associé
Auditra::manual('system_maintenance', null, [
    'metadata' => ['action' => 'cache_cleared']
]);

// Récupérer tous les audits d'un modèle
$user = User::find(1);
$audits = $user->auditLogs;

// Filtrer par événement
$audits = \Darken10\Auditra\Models\AuditLog::byEvent('updated')->get();

// Filtrer par utilisateur
$audits = \Darken10\Auditra\Models\AuditLog::byUser(auth()->id())->get();

// Filtrer par modèle
$audits = \Darken10\Auditra\Models\AuditLog::byModel(User::class)->get();

// Filtrer par période
$audits = \Darken10\Auditra\Models\AuditLog::whereBetween('created_at', [
    now()->subDays(7),
    now()
])->get();
bash
php artisan vendor:publish --provider="Darken10\Auditra\AuditraServiceProvider"
bash
php artisan migrate