Weave 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

Easily log user activities in your Laravel app and automatically track model events. Store everything in the activity_log table, then retrieve and query activities with the Activity model and a fluent API.

View on GitHub
Deep Wiki
Context7

spatie/laravel-activitylog provides simple, fluent helpers to log user activity inside your Laravel application, storing every entry in the activity_log table. Log anything with activity()->log('...'), or attach rich context—the subject model, the causer, and custom properties—then retrieve records through the Activity model.

It can also automatically log model events like create, update, and delete, so you get a full audit trail without writing extra code.

  • Fluent API for performedOn, causedBy, and withProperties
  • Automatic logging of Eloquent model events
  • Retrieve activities and custom properties via the Activity model
  • Configurable table, model, and activity cleaning
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.
jinas/dhivatar
store-doc/laravel-storage-driver
plus-it/laravel-translation-manager
diephp/perhaps
diephp/telegraph
osmaviation/spreadsheet
microsoft/fluentui-system-icons
mmccook/php-json-canonicalization-scheme
middlewares/firewall
qubiqx/montapacking
nyoncode/laravel-package-console
pnscripts/filament-broadquill-lite
pnscripts/laravel-sentfold-lite
vleap/mx-sdk-laravel-internal
twidpay/utils
htetmyathlaing/scout-elasticsearch-driver
artisan-toolbox/front-awesome
ls/client-assistant
users-au/laravel-client
cgv/localized-resource