motomedialab/simple-laravel-audit
Installation:
composer require motomedialab/simple-laravel-audit
php artisan migrate
This creates the audit_logs table and sets up the package.
First Use Case: Trigger an audit log for a model update in a controller or service:
use Motomedialab\SimpleLaravelAudit\Facades\Audit;
// Example: Audit a user update
Audit::log('users.update', $user, 'Updated user profile', ['old_email' => $user->old_email]);
Where to Look First:
Motomedialab\SimpleLaravelAudit\Facades\Audit (primary entry point).php artisan vendor:publish --provider="Motomedialab\SimpleLaravelAudit\AuditServiceProvider" to customize table names, IP resolution, or user resolution.database/migrations/[timestamp]_create_audit_logs_table.php for schema details.Model Events:
Bind to Eloquent events (e.g., saved, deleted) to auto-audit changes:
use Motomedialab\SimpleLaravelAudit\Facades\Audit;
$user->saved(function ($model) {
Audit::log('users.created', $model, 'User created via registration');
});
Manual Logging: Log custom actions (e.g., API calls, admin actions):
Audit::log(
'admin.action',
auth()->user(),
'Deleted 5 inactive users',
['count' => 5, 'model' => 'User']
);
FilamentPHP Integration:
Use the HasAuditLogs trait in Filament resources to display audit trails:
use Motomedialab\SimpleLaravelAudit\Filament\HasAuditLogs;
class UserResource extends Resource {
use HasAuditLogs;
// ...
}
Batch Operations: Audit bulk actions (e.g., imports) with a single log entry:
Audit::log('import.users', auth()->user(), 'Imported 100 users', ['file' => 'users.csv']);
public function handle(Request $request, Closure $next) {
Audit::log('http.request', auth()->user(), $request->path(), ['method' => $request->method()]);
return $next($request);
}
Audit::queue()->log('slow.process', $user, 'Processed large dataset');
'user_resolver' => function () {
return auth()->guard('admin')->user();
},
Performance:
if (app()->environment('production')) {
Audit::log('critical.action', $user, '...');
}
Data Leakage:
Audit::log('users.update', $user, 'Updated profile', [
'old_email' => $user->old_email,
'new_email' => str_replace('*@*.com', '***@***.com', $user->email),
]);
Filament Caching:
php artisan filament:cache-clear
Model Binding:
Missing Logs:
audit_logs table exists and is writable.'users.update') matches your config or queries.'debug' => env('APP_DEBUG', false),
IP Resolution:
::1 or 127.0.0.1, override the ip_resolver in config:
'ip_resolver' => function () {
return request()->ip() ?? 'external';
},
Custom Fields:
Add columns to the audit_logs table via a migration:
Schema::table('audit_logs', function (Blueprint $table) {
$table->string('custom_field')->nullable();
});
Then extend the Audit facade to include it:
Audit::log('custom.event', $user, 'Action', [], ['custom_field' => 'value']);
Event Subscribers:
Listen to audit.logged events to process logs asynchronously:
Event::listen(AuditLogged::class, function (AuditLogged $event) {
// Send notification, update analytics, etc.
});
Soft Deletes:
Exclude soft-deleted models from audit logs by overriding the shouldAudit method in your model:
public function shouldAudit(): bool {
return !$this->isSoftDeleted();
}
Localization: Translate log messages using Laravel’s translation system:
Audit::log('users.update', $user, __('audit.users.updated'));
How can I help you explore Laravel packages today?