Installation
composer require damienharper/auditor
Publish the config and migration:
php artisan vendor:publish --provider="DamienHarper\Auditor\AuditorServiceProvider" --tag="auditor-config"
php artisan vendor:publish --provider="DamienHarper\Auditor\AuditorServiceProvider" --tag="auditor-migrations"
Run migrations:
php artisan migrate
Basic Setup
Add the Auditable trait to your Eloquent model:
use DamienHarper\Auditor\Traits\Auditable;
class User extends Model
{
use Auditable;
}
First Audit Log Trigger an audit event by modifying a model:
$user = User::find(1);
$user->name = 'Updated Name';
$user->save(); // Automatically logs changes
config/auditor.php (adjust log retention, drivers, etc.).database/migrations/[timestamp]_create_audit_logs_table.php (customize the schema).\Auditor::log() for manual logging.\DamienHarper\Auditor\Middleware\AuditLogs to log API requests.Automatic Model Auditing
Auditable trait.$auditIgnore:
protected $auditIgnore = ['password', 'remember_token'];
Manual Logging Use the facade for non-model events:
\Auditor::log('system.event', [
'user_id' => auth()->id(),
'action' => 'exported_report',
'details' => $reportData,
]);
Policy-Based Auditing Restrict auditing to specific actions in policies:
public function update(User $user, User $model)
{
if ($user->isAdmin()) {
return true;
}
\Auditor::log('policy.violation', ['user_id' => $user->id]);
return false;
}
API Request Auditing Use middleware to log HTTP requests:
protected $middleware = [
\DamienHarper\Auditor\Middleware\AuditLogs::class,
];
Customize payload in AuditLogs middleware:
public function handle($request, Closure $next)
{
$request->auditData = ['custom_field' => 'value'];
return $next($request);
}
Batch Operations Disable auditing for bulk operations to avoid performance hits:
\Auditor::disable();
User::where('active', false)->update(['active' => true]);
\Auditor::enable();
audit.logged for post-audit actions:
Event::listen('audit.logged', function ($log) {
// Send notification, trigger workflows, etc.
});
\Auditor::setQueueConnection('database');
AuditLog model to support soft deletes:
use Illuminate\Database\Eloquent\SoftDeletes;
class AuditLog extends \DamienHarper\Auditor\Models\AuditLog
{
use SoftDeletes;
}
Performance Overhead
Auditor::disable() for bulk operations or disable for non-critical models.Ignored Attributes
password) may still leak if not explicitly ignored.$auditIgnore in models:
protected $auditIgnore = ['password', 'api_token'];
Database Bloat
config/auditor.php:
'retention' => [
'enabled' => true,
'days' => 30,
],
Middleware Conflicts
$request->auditData = array_merge($request->auditData ?? [], ['custom_field' => 'value']);
Custom Drivers
Driver class or use the database driver for full functionality.Auditor::log() with severity levels:
\Auditor::log('event.name', [], 'error'); // 'info', 'warning', 'error'
DB::enableQueryLog();
$user->save();
dd(DB::getQueryLog());
retrieved, saved, etc., to debug audit triggers:
protected static function bootAuditable()
{
static::saved(function ($model) {
logger()->debug('Auditing saved model:', ['model' => $model]);
});
}
Custom AuditLog Model Extend the base model to add fields:
class CustomAuditLog extends \DamienHarper\Auditor\Models\AuditLog
{
protected $casts = [
'metadata' => 'array',
];
}
Update the config:
'model' => \App\Models\CustomAuditLog::class,
Custom Drivers
Implement DamienHarper\Auditor\Contracts\Driver for non-database storage (e.g., Elasticsearch):
class ElasticDriver implements Driver
{
public function log(array $data) { /* ... */ }
}
Register in config:
'driver' => \App\Drivers\ElasticDriver::class,
Dynamic Ignore Attributes Use a closure to conditionally ignore attributes:
protected $auditIgnore = [
'password',
function ($model) {
return $model->isSensitive ? ['secret_key'] : [];
},
];
Audit Log Filters
Add scopes to the AuditLog model for querying:
public function scopeForModel($query, $model)
{
return $query->where('model', get_class($model));
}
How can I help you explore Laravel packages today?