- How do I install and set up Laravel Auditing in a Laravel 13 project?
- Run `composer require owen-it/laravel-auditing` and publish the config with `php artisan vendor:publish --provider="OwenIt\LaravelAuditing\AuditingServiceProvider"`. Then add the `HasAuditing` trait to your Eloquent models and run the provided migration to create the `audits` table.
- Which Laravel versions does this package officially support?
- The package supports Laravel 11.x, 12.x, and 13.x with PHP 8.2+. Older versions (e.g., Laravel 10) may work with pinned releases but lack active support or security updates. Check the [documentation](https://laravel-auditing.com) for version-specific details.
- Can I exclude specific fields or events from being audited?
- Yes, you can exclude fields by defining an `exclude` property in your model (e.g., `protected $exclude = ['password', 'api_token']`). To exclude events like `retrieved`, override the `auditEvents` method in your model to return only the desired events (e.g., `['created', 'updated', 'deleted']`).
- How does Laravel Auditing handle multi-user environments (e.g., SaaS platforms)?
- The package automatically tracks the user responsible for changes via Laravel’s `auth` system. For custom user resolution (e.g., API tokens or service accounts), use the `resolveUser` resolver in the config or override the `resolveUser()` method in your model to return the appropriate user ID or identifier.
- Will auditing impact performance in high-traffic applications?
- Yes, audit logs add write overhead to Eloquent operations. For high-throughput systems, disable auditing for non-critical models or batch writes using Laravel queues. Optimize read performance with database indexing (e.g., `auditable_id`, `created_at`) and caching frequent queries (e.g., Redis for recent changes).
- Does this package support custom audit drivers (e.g., Elasticsearch or MongoDB)?
- The package primarily supports a database driver by default, but you can extend it with custom drivers by implementing the `OwenIt\LaravelAuditing\Contracts\AuditDriver` interface. The documentation includes examples for creating custom drivers, though the database driver remains the most widely used and maintained option.
- How do I retrieve and display audit logs for a specific model?
- After adding the `HasAuditing` trait to your model, you can access audit logs via `$model->audits`. For example, `User::find(1)->audits` returns a collection of audit entries. Use Laravel’s pagination (e.g., `->paginate(10)`) or query scopes (e.g., `->where('event', 'updated')`) to filter results efficiently.
- Are there alternatives to Laravel Auditing for Laravel audit trails?
- Yes, alternatives include `spatie/laravel-activitylog` (feature-rich but heavier) and `laravel-model-auditing` (simpler but less maintained). Laravel Auditing stands out for its seamless Eloquent integration, minimal boilerplate, and support for dynamic resolvers. Compare based on your needs—e.g., choose `spatie/laravel-activitylog` for advanced features like tagging or `laravel-auditing` for lightweight, trait-based auditing.
- How can I test audit logs in my Laravel application?
- Use Laravel’s testing tools to simulate model changes. For example, in a PHPUnit test, create a model, update it, and assert the audit log entries: `$user = User::create(...); $user->update(['name' => 'New Name']); $this->assertCount(1, $user->audits->where('event', 'updated'))`. Mock the `resolveUser()` method if testing custom user resolution logic.
- What should I do if audit logs are missing or incomplete?
- Check for middleware or custom logic that might skip Eloquent events (e.g., `saving`, `updating`). Ensure the `HasAuditing` trait is applied to all relevant models and that the `audits` table exists. Monitor for gaps in `created_at` timestamps or missing events, and validate with unit tests for critical models. If using queues, verify they’re processing audit jobs.