taldres/laravel-last-seen
最新稳定版本:v0.3.1
Composer 安装命令:
composer require taldres/laravel-last-seen
包简介
Laravel package to track user's last seen status.
README 文档
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_attimestamp 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\LastSeentrait to your User model:use Taldres\LastSeen\Trait\LastSeen; class User extends Authenticatable { use LastSeen; // ... }
-
Add the middleware to your
weborapimiddleware 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 theupdateLastSeenAtmethod 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(): Returnstrueif 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
统计信息
- 总下载量: 193
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-07-25