spatie/activitylog
Laravel 5 user activity logging package by Spatie: records actions to a database table and optionally to Laravel’s log handler, with migration and facade support. Abandoned since 2016-06-28; use spatie/laravel-activitylog instead.
Installation:
composer require spatie/activitylog
Publish the migration:
php artisan vendor:publish --provider="Spatie\Activitylog\ActivitylogServiceProvider" --tag="migrations"
Run the migration:
php artisan migrate
Basic Logging:
Use the logActivity() helper in controllers or services:
use Spatie\Activitylog\LogOptions;
logActivity('User updated profile', new LogOptions());
First Use Case:
Log a model event (e.g., updated) for a User model:
use Spatie\Activitylog\Traits\LogsActivity;
class User extends Model
{
use LogsActivity;
}
Model Observers: Attach observers to models for automatic logging:
class UserObserver extends Observer
{
public function saved(Model $model)
{
$model->logActivity('Profile updated');
}
}
Customizing Logs:
Override getActivityDescriptionForEvent() in models:
public function getActivityDescriptionForEvent($event)
{
return "{$event}: {$this->name} (ID: {$this->id})";
}
Logging with Metadata:
Pass additional data via LogOptions:
logActivity('Order created', new LogOptions([
'properties' => ['order_id' => $order->id, 'amount' => $order->amount],
]));
Logging to Log Handler:
Enable in config/activitylog.php:
'log' => env('ACTIVITYLOG_LOG', false),
Querying Logs:
Use the Activity model to fetch logs:
$logs = \Spatie\Activitylog\Models\Activity::where('subject_type', 'App\User')->latest()->get();
SoftDeletes if tracking deleted events.event(new OrderCreated($order));
// In listener: logActivity('Order created', ...);
public function handle($request, Closure $next)
{
logActivity('API request', new LogOptions(['properties' => $request->all()]));
return $next($request);
}
Deprecated Package:
spatie/laravel-activitylog instead for Laravel 5.5+.resolvingModel()).Migration Conflicts:
activities table may conflict with other packages. Check for column overlaps (e.g., properties JSON field).Performance:
retrieved) can bloat the database. Use only in config/activitylog.php:
'only' => ['created', 'updated', 'deleted'],
Circular References:
properties may cause serialization issues. Use IDs or arrays:
'properties' => ['user_id' => $user->id, 'tags' => $user->tags->pluck('name')],
Missing Logs:
activities table exists and the logActivity() call is reached.config/activitylog.php for only/except filters.LogsActivity trait.Querying Issues:
toSql() on queries to debug:
\Spatie\Activitylog\Models\Activity::where('subject_type', 'App\User')->toSql();
Custom Log Table:
Override the Activity model to use a different table:
class Activity extends \Spatie\Activitylog\Models\Activity
{
protected $table = 'custom_activity_logs';
}
Custom Log Model:
Extend the Activity model to add fields:
class Activity extends \Spatie\Activitylog\Models\Activity
{
protected $casts = ['ip_address' => 'string'];
}
Logging to External Services:
Override the log() method in the ActivitylogServiceProvider to forward logs to an API or queue.
laravel-auditlog or owen-it/auditing for richer features (e.g., diffs, nested models).properties before logging:
'properties' => ['email' => str_replace('@', '[at]', $user->email)],
Activity::flush() to clear logs between tests:
public function tearDown(): void
{
\Spatie\Activitylog\Models\Activity::flush();
}
How can I help you explore Laravel packages today?