taldres/laravel-last-seen
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 | PHP Version |
|---|---|---|
^11.15 |
^0.4 |
^8.2 |
^12.0 |
^0.4 |
^8.2 |
^13.0 |
^0.4 |
^8.3 |
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
taldres/laravel-last-seen 适用场景与选型建议
taldres/laravel-last-seen 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 355 次下载、GitHub Stars 达 1, 最近一次更新时间为 2025 年 07 月 25 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 taldres/laravel-last-seen 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 taldres/laravel-last-seen 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 355
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 14
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-07-25