taldres / laravel-last-seen
Laravel package to track user's last seen status.
Requires
- php: ^8.2
- illuminate/database: ^v11.44.2|^12.0
- illuminate/filesystem: ^v11.44.2|^12.0
- illuminate/support: ^v11.44.2|^12.0
Requires (Dev)
- larastan/larastan: ^3.6.0
- laravel/pint: ^1.24.0
- orchestra/testbench: ^9.0|^10.4
- pestphp/pest: ^3.8.2
- pestphp/pest-plugin-laravel: ^3.2
- pestphp/pest-plugin-type-coverage: ^3.6
README
Laravel Last Seen
A simple Laravel package to track a user's last seen and recently seen status. This package provides traits, middleware, events, and configuration to easily record and query when a user was last active in your Laravel application.
Features
- Automatically update the
last_seen_at
timestamp for users - Middleware to detect user activity
- Event-based architecture for extensibility
- Query scopes and helper methods to check if a user was recently seen
- Configurable thresholds for updating and checking activity
- Migration publishing for easy setup
Requirements
PHP
PHP 8.2 or higher
Supported Laravel Versions
Laravel Version | Package Version |
---|---|
^11.15 |
^0.2 |
^12.0 |
^0.2 |
Installation
-
Install the package via Composer:
composer require taldres/laravel-last-seen
-
Publish the migration and configuration files:
php artisan vendor:publish --provider="Taldres\LastSeen\LastSeenServiceProvider"
-
Clear the configuration cache to ensure the new settings are loaded:
php artisan optimize:clear # or php artisan config:clear
-
Run the migration to create the necessary database table:
php artisan migrate
-
Add the
Taldres\LastSeen\Trait\LastSeen
trait to your User model:use Taldres\LastSeen\Trait\LastSeen; class User extends Authenticatable { use LastSeen; // ... }
-
Add the middleware to your
web
orapi
middleware group or any other endpoint:// ... \Taldres\LastSeen\Middleware\UpdateLastSeenMiddleware::class, // ...
Configuration
If necessary or in case of a newer version, you can publish the configuration file to customize the package settings:
php artisan vendor:publish --provider="Taldres\LastSeen\LastSeenServiceProvider" --tag="config"
or this command to force overwrite the existing configuration file:
php artisan vendor:publish --provider="Taldres\LastSeen\LastSeenServiceProvider" --tag="config" --force
In the config/last-seen.php
file, you can specify the User model to be used for tracking last seen timestamps:
user
: The fully qualified class name of the User model to be used for tracking last seen timestamps.
All other settings—such as enabling/disabling the feature, update thresholds, and recently seen thresholds—can be controlled via environment variables in your .env
file:
LAST_SEEN_ENABLED
: Enables or disables the package globally (default: true). It affects only theupdateLastSeenAt
method and the middleware.LAST_SEEN_UPDATE_THRESHOLD
: Minimum seconds between last_seen_at updates (default: 60)LAST_SEEN_RECENTLY_SEEN_THRESHOLD
: Seconds a user is considered recently seen after last activity (default: 300)
Each setting has a default value, so you only need to override them if you want to change the default behavior.
Usage
Checking Activity
$user->recentlySeen()
: Returnstrue
if the user was active within the configured threshold.User::onlyRecentlySeen()
: Query scope to get only users recently seen.
Events
The package fires a UserWasActiveEvent
whenever user activity is detected. You can listen to this event for custom logic.
Manually Dispatching the Event
You can also dispatch the UserWasActiveEvent
from your own application code:
use Taldres\LastSeen\Events\UserWasActiveEvent; use Illuminate\Support\Facades\Event; Event::dispatch(new UserWasActiveEvent($user));
License
MIT