garcia1901l/laravel-activity-lite
Lightweight MongoDB-backed activity logger for Laravel (7–12). Automatically tracks model create/update/delete, records the causer (user, artisan, queues), supports manual log entries, configurable events, and powerful querying with CLI filters and JSON/CSV export.
Installation:
composer require garcia1901l/laravel-activity-lite
php artisan activity-lite:install
Enable Tracking for a Model:
Add the ActivityTrait to your Eloquent model:
use Garcia1901l\LaravelActivityLite\Traits\ActivityTrait;
class Post extends Model
{
use ActivityTrait;
}
First Use Case: Log an activity manually (e.g., in a controller or observer):
use Garcia1901l\LaravelActivityLite\Facades\ActivityLite;
ActivityLite::log('post.updated', $post, 'Custom description');
$activities = ActivityLite::query()
->whereModel('App\Models\Post')
->whereEvent('post.updated')
->get();
Automatic Model Tracking:
ActivityTrait. The package auto-logs created, updated, and deleted events.activityEvents property:
protected $activityEvents = ['created', 'updated', 'deleted', 'published'];
Manual Logging:
ActivityLite::log('user.login', $user, 'Logged in via API', ['ip' => $request->ip()]);
Querying Activities:
ActivityLite::query()
->whereModel('App\Models\User')
->whereEvent('user.login')
->whereCauser('admin@test.com')
->orderBy('created_at', 'desc')
->limit(10)
->get();
$activity = ActivityLite::query()->first();
Integration with Observers/Events:
public function saving(Model $model)
{
if ($model->isDirty('status')) {
ActivityLite::log('post.status_changed', $model);
}
}
Causer Tracking:
auth()->user() or app()->runningInConsole()).ActivityLite::log('manual.event', $model, null, null, 'custom_causer@example.com');
Activity Scopes:
class ActivityScopes
{
public static function recent($days = 7)
{
return ActivityLite::query()
->where('created_at', '>=', now()->subDays($days));
}
}
Batch Processing:
ActivityLite::logBatch([
['event' => 'import.started', 'model' => $model, 'description' => 'Bulk import'],
['event' => 'items.created', 'model' => $model, 'count' => 100],
]);
Activity-Based Notifications:
$activities = ActivityLite::query()
->whereEvent('order.paid')
->where('created_at', '>=', now()->subHours(1))
->get();
foreach ($activities as $activity) {
Notification::send($activity->causer, new OrderPaid($activity));
}
MongoDB Connection:
mongodb/laravel-mongodb is installed and configured in .env:
MONGO_CONNECTION=default
MONGO_DATABASE=activity_lite
composer.json to avoid conflicts.Causer Resolution:
auth()->user() for web requests and app()->runningInConsole() for CLI. Override in config/activity-lite.php if your auth system differs:
'causer_resolver' => function () {
return auth('api')->user() ?? 'system';
},
Event Naming:
user.profile.updated) to avoid collisions. Avoid spaces or special characters.Performance:
property.changed for every field update). Batch or debounce logs where possible.event, model, created_at).Model Serialization:
activityAttributes to limit logged fields:
protected $activityAttributes = ['id', 'title', 'status'];
Log Inspection:
dd(ActivityLite::query()->get()->toArray());
mongosh --eval 'db.activity_lite.find().pretty()'
Common Issues:
activity_lite collection exists (run php artisan activity-lite:install again).select(), where(), or limit() to avoid fetching large datasets.Custom Activity Model:
Activity model to add fields:
namespace App\Models;
use Garcia1901l\LaravelActivityLite\Models\Activity as BaseActivity;
class Activity extends BaseActivity
{
protected $casts = [
'metadata' => 'array',
'tags' => 'array',
];
}
'model' => App\Models\Activity::class,
Activity Events:
ActivityLite::created(function ($activity) {
if ($activity->event === 'user.created') {
// Send welcome email
}
});
Middleware for API Tracking:
public function handle($request, Closure $next)
{
$response = $next($request);
ActivityLite::log(
'api.request',
null,
$request->method() . ' ' . $request->path(),
['ip' => $request->ip(), 'user_agent' => $request->userAgent()]
);
return $response;
}
Activity Exporters:
$activities = ActivityLite::query()
->where('created_at', '>=', now()->subDays(30))
->get()
->toArray();
file_put_contents('activity_log.json', json_encode($activities));
How can I help you explore Laravel packages today?