- How do I automatically log Eloquent model changes in Laravel using this package?
- Use the `LogsActivity` trait on your Eloquent models. After running the migration, add `use LogsActivity;` to your model and it will automatically log changes like `created`, `updated`, and `deleted` events. Customize behavior with methods like `logOnly()` or `dontLogIfAttributesChangedOnly()`.
- Can I log custom events or actions not tied to Eloquent models?
- Yes. Use the `activity()` facade to manually log actions. For example, `activity()->log('Order processed')` or chain methods like `performedOn($order)->causedBy($user)->withProperties(['status' => 'paid'])->log('Payment received')` for richer context.
- What Laravel versions does `spatie/laravel-activitylog` support?
- The package officially supports Laravel 8 through 11. Check the [GitHub releases](https://github.com/spatie/laravel-activitylog/releases) for version-specific compatibility notes. It’s tested against the latest Laravel LTS releases, ensuring stability.
- How do I filter or query logged activities efficiently?
- Use the `Activity` model’s query builder methods like `whereDescription()`, `whereCauserId()`, or `whereSubjectType()`. For complex queries, leverage Eloquent relationships (e.g., `Activity::whereHas('causer', fn($q) => $q->where('role', 'admin'))`). Add indexes to `activity_log` columns for performance.
- Does this package work with UUIDs instead of auto-incrementing IDs?
- No, the default migration assumes integer IDs. For UUIDs, manually adjust the `activity_log` migration to use `Morphs` or `Uuids` traits. Alternatively, create a custom migration or use a package like `spatie/laravel-uuid` to handle UUIDs globally.
- How can I reduce performance overhead from logging every model change?
- Use `logOnly()` to specify only critical attributes or `dontLogIfAttributesChangedOnly()` to skip trivial updates. For high-throughput systems, offload logging to a queue with `Activity::shouldQueue()` or batch writes using the `beforeLogging` hook to group related activities.
- Is there a way to archive or purge old activity logs automatically?
- Yes. Use Laravel’s scheduler to run a command like `Activity::where('created_at', '<=', now()->subDays(30))->delete()`. For large datasets, consider partitioning the `activity_log` table or using a separate database for archived logs.
- Can I customize the `activity_log` table schema or add columns?
- Extend the default migration by publishing it with `php artisan vendor:publish --tag=activitylog-migrations` and modifying the `up()` method. Add columns like `ip_address` or `metadata` via JSON fields, but ensure backward compatibility for future updates.
- How do I log activities caused by non-user entities (e.g., cron jobs, APIs)?
- Use the `causedBy()` method with any Eloquent model, even a custom one. For APIs, pass the request’s user or a dedicated `SystemUser` model. For cron jobs, create a static model instance like `new SystemUser()` to associate activities with the job.
- Are there alternatives to this package for Laravel audit logging?
- Yes. Consider `owen-it/laravel-auditing` for granular model-level auditing or `laravel-audit-log` for more advanced features like diff tracking. However, `spatie/laravel-activitylog` stands out for its simplicity, performance optimizations, and flexibility in logging both model events and custom actions.