Kritim Yantra
Apr 16, 2025
In this blog post, we'll explore how to integrate and use the Spatie Laravel-Activitylog package in Laravel 12 to track user activities, log model changes, and monitor application events efficiently.
The Spatie Laravel-Activitylog package provides an easy way to log user activities, model changes, and custom events in Laravel. Key features include:
If you donβt have Laravel 12 installed:
composer create-project laravel/laravel activitylog-demo
cd activitylog-demo
Install the package via Composer:
composer require spatie/laravel-activitylog
Publish the migration and config file:
php artisan vendor:publish --provider="Spatie\Activitylog\ActivitylogServiceProvider" --tag="migrations"
php artisan migrate
Publish the config file (optional):
php artisan vendor:publish --provider="Spatie\Activitylog\ActivitylogServiceProvider" --tag="config"
use Spatie\Activitylog\Models\Activity;
activity()->log('User logged in');
This stores a log entry in the activity_log
table.
activity()
->causedBy(auth()->user()) // Associate with a user
->withProperties(['ip' => request()->ip()])
->log('User viewed dashboard');
$activities = Activity::all();
foreach ($activities as $activity) {
echo $activity->description; // "User logged in"
echo $activity->causer->name; // User who triggered it
}
Automatically log created, updated, and deleted events for Eloquent models.
LogsActivity
Traituse Spatie\Activitylog\Traits\LogsActivity;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
use LogsActivity;
protected static $logAttributes = ['title', 'content']; // Track changes to these fields
protected static $logOnlyDirty = true; // Only log if values change
}
When a Post
is updated:
$post = Post::find(1);
$post->update(['title' => 'New Title']);
// Retrieve logs
$lastActivity = $post->activities()->latest()->first();
echo $lastActivity->description; // "updated"
echo $lastActivity->changes; // {"old": {"title": "Old Title"}, "new": {"title": "New Title"}}
protected static $logName = 'custom_log';
protected static $recordEvents = ['created', 'deleted']; // Skip 'updated'
public function shouldLogActivity(string $eventName): bool
{
return $eventName === 'updated' && $this->isDirty('status');
}
$post = Post::find(1);
$activities = $post->activities;
use Spatie\Activitylog\Models\Activity;
// Get all login activities
Activity::where('description', 'User logged in')->get();
// Get activities by a user
Activity::causedBy($user)->get();
Activity::where('created_at', '<', now()->subDays(30))->delete();
To avoid performance issues, log activities in a queue:
// In `config/activitylog.php`
'dispatch_events_to_queue' => true,
Extend the default Activity
model:
use Spatie\Activitylog\Models\Activity;
class CustomActivity extends Activity
{
// Custom logic
}
Update config:
'activity_model' => CustomActivity::class,
protected static $logAttributesToIgnore = ['updated_at'];
The Spatie Laravel-Activitylog package is a powerful tool for:
β
Tracking user actions (logins, updates).
β
Auditing model changes (created, updated, deleted).
β
Debugging & analytics (query logs for insights).
Integrate it into your Laravel 12 app today for better monitoring!
Happy logging! π
No comments yet. Be the first to comment!
Please log in to post a comment:
Sign in with Google