lvlup-dev / laravel-user-last-seen-at
Adds a nullable last_seen_at column on users and middleware to update it on each authenticated request.
Package info
github.com/lvlup-dev/laravel-user-last-seen-at
Type:laravel-package
pkg:composer/lvlup-dev/laravel-user-last-seen-at
Requires
- php: ^8.2
- illuminate/auth: ^10.0|^11.0|^12.0|^13.0
- illuminate/contracts: ^10.0|^11.0|^12.0|^13.0
- illuminate/database: ^10.0|^11.0|^12.0|^13.0
- illuminate/http: ^10.0|^11.0|^12.0|^13.0
- illuminate/routing: ^10.0|^11.0|^12.0|^13.0
- illuminate/support: ^10.0|^11.0|^12.0|^13.0
Requires (Dev)
- orchestra/testbench: ^9.0|^10.0|^11.0
- pestphp/pest: ^2.34|^3.0|^4.0
- pestphp/pest-plugin-laravel: ^2.4|^3.0|^4.0
README
Store a last_seen_at timestamp on your users table and refresh it on each request for authenticated users—useful for “online recently” indicators or light activity tracking without extra tables.
Maintained by Lvlup.
Installation
You can install the package via composer:
composer require lvlup-dev/laravel-user-last-seen-at
Run the migrations (the package registers its migration automatically):
php artisan migrate
Usage
Registering the Middleware
You need to append the middleware to your route groups (e.g., the web group). In Laravel 11+, you can do this in your bootstrap/app.php file:
// bootstrap/app.php ->withMiddleware(function (Illuminate\Foundation\Configuration\Middleware $middleware): void { // Using the class name... $middleware->web(append: [ \LvlupDev\UserLastSeenAt\Http\Middleware\UserLastSeen::class, ]); // ...or using the alias registered by this package $middleware->web(append: [ 'lastSeenAt', ]); })
Note: Guests are automatically skipped, and no database write occurs for unauthenticated users.
Your User model
This package does not ship a custom User model—you keep yours.
The middleware updates the timestamp using forceFill and saveQuietly. This means:
last_seen_atdoes not need to be added to your$fillablearray.- It will not trigger any
updatedorsavedEloquent events, saving performance and preventing infinite loops in observers.
You may still want to cast it as a datetime in your User model for convenience:
protected function casts(): array { return [ 'last_seen_at' => 'datetime', ]; }
Alternatives
laravel-user-last-seen-at is intentionally minimal: one nullable column and one middleware.
If you need comprehensive audit trails, rich activity feeds, or per-model event history, we recommend using spatie/laravel-activitylog or building a custom event-driven design.
Credits
laravel-user-last-seen-at is built and maintained by LVLUP. We help businesses drive operational efficiency through strategic consulting, tailored software development, and advanced AI Agent integrations.
Read this blog post (in French) on why we built this package to keep activity tracking pragmatic and efficient.
License
The MIT License (MIT). Please see License File for more information.