taldres/laravel-last-seen 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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

Packagist Version Tests

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

  1. Install the package via Composer:

    composer require taldres/laravel-last-seen
  2. Publish the migration and configuration files:

    php artisan vendor:publish --provider="Taldres\LastSeen\LastSeenServiceProvider"
  3. Clear the configuration cache to ensure the new settings are loaded:

    php artisan optimize:clear
    # or
    php artisan config:clear
  4. Run the migration to create the necessary database table:

    php artisan migrate
  5. Add the Taldres\LastSeen\Trait\LastSeen trait to your User model:

    use Taldres\LastSeen\Trait\LastSeen;
    
    class User extends Authenticatable
    {
        use LastSeen;
        // ...
    }
  6. Add the middleware to your web or api 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 the updateLastSeenAt 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(): Returns true 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

统计信息

  • 总下载量: 193
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-07-25