Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message
Laravel Activitylog

Laravel Activitylog Laravel Package

spatie/laravel-activitylog

Log user and model activity in Laravel with a simple API. Automatically record Eloquent model events, link actions to subjects and causers, store custom properties, and query a dedicated activity_log table for auditing and history.

View on GitHub
Deep Wiki
Context7

Log activity inside your Laravel app

Frequently asked questions about Laravel Activitylog
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.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
codraw/framework-extra-bundle
codraw/messenger
codraw/security
codraw/mailer
codraw/contracts
codraw/profiling
codraw/dependency-injection
codraw/tester
codraw/core
nexmo/api-specification
capell-app/block-library
axium/identity
cetria/laravel-dummy-models
cetria/reflection-helper
agropredict/sso-auth-bundle
evolvestudio/spam-protection
datacore/hub-sdk
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony