brianclogan / laravel-user-status
自动跟踪用户状态,或设置自定义状态。
dev-main
2024-07-08 01:17 UTC
Requires
- php: ^8.2
- illuminate/contracts: ^10.0||^11.0
- spatie/laravel-package-tools: ^1.16
Requires (Dev)
- larastan/larastan: ^2.9
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.1.1||^7.10.0
- orchestra/testbench: ^9.0.0||^8.22.0
- pestphp/pest: ^2.34
- pestphp/pest-plugin-arch: ^2.7
- pestphp/pest-plugin-laravel: ^2.3
- phpstan/extension-installer: ^1.3
- phpstan/phpstan-deprecation-rules: ^1.1
- phpstan/phpstan-phpunit: ^1.3
- spatie/laravel-ray: ^1.35
README
自动跟踪用户状态,或由用户设置自定义状态。
最初,这个包是为用户准备的,但我意识到它可以用于任何你想跟踪状态的模型(例如:团队)。
因此,虽然它被称为 Laravel User Status,但它可以用于任何你想跟踪状态的模型。
功能
- 自动跟踪用户状态
- 自定义状态
- 实时状态更新
- 保留状态历史
- 可定制
- Laravel Echo 支持
安装
您可以通过 composer 安装此包
composer require brianclogan/laravel-user-status
您可以使用以下命令发布和运行迁移
php artisan vendor:publish --tag="laravel-user-status-migrations"
php artisan migrate
您可以使用以下命令发布配置文件
php artisan vendor:publish --tag="laravel-user-status-config"
这是已发布的配置文件的内容
return [ /** * Tables * * The table names used by the package. */ 'tables' => [ 'status_table' => env('USER_STATUS_TABLE', 'user_statuses'), ], /** * Laravel Echo Configuration * * This is used to broadcast the status changes to the frontend * and update the status in real-time. (if enabled) */ 'echo' => [ /** * Enable or disable the broadcasting of the status changes */ 'enabled' => env('USER_STATUS_ECHO_ENABLED', false), 'channel' => 'statusable.{type}.{id}', /** * Enable or disable the broadcasting of the presence changes */ 'presences_enabled' => env('USER_STATUS_PRESENCES_ENABLED', false), 'presences' => 'statusable.{type}.{id}.presences', ], /** * Middleware * * This is applied when a user makes a request. It will update the user's status * based on the configuration below. * * If you want to disable this, set `enabled` to false. * * If you want to apply this to a different group, you can add more groups to the `groups` array. * If you apply `api`, it will set the user status online for any API request which is not recommended. * * Feel free to disable this, and make your own middleware if you want. */ 'middleware' => [ 'enabled' => env('USER_STATUS_MIDDLEWARE_ENABLED', true), 'groups' => [ 'web', ], 'status' => 'online', 'reason' => 'active', 'meta' => null, ], /** * Status Model * * The model used to store the statuses, you can extend the model * and change the class here. NOT RECOMMENDED, but possible. */ 'status_model' => \BrianLogan\LaravelUserStatus\Models\Status::class, /** * Keep History * * If enabled, the package will keep past statuses in the database. * * This is useful for analytics and other purposes, but is disabled * by default to reduce the size of the database. * * If you enable this, you should also enable the `echo.enabled` option * to keep the frontend in sync with the backend. * * This will update the status model to use a morphMany relationship * instead of a morphOne relationship. */ 'keep_history' => env('USER_STATUS_KEEP_HISTORY', false), ];
用法
将 HasStatus
特性添加到你想要跟踪状态的模型中。
use BrianLogan\LaravelUserStatus\Traits\HasStatus; class User extends Model { use HasStatus; }
获取最新状态
$user = User::find(1); $user->getLatestStatus();
设置状态
当调用 setStatus
时,只需要 status
即可。 reason
和 meta
是可选的。
meta
可以用于存储有关状态的附加信息,例如颜色、图标、自定义消息等。
$user = User::find(1); $user->setStatus(status: 'active', reason: 'User is active', meta: ['foo' => 'bar']);
注意:如果您启用了
keep_history
,而不是更新状态,它将创建一条新记录。
作用域
获取具有特定状态的所有记录
User::whereStatus('active')->get();
注意:如果您启用了
keep_history
,它将返回所有具有该状态记录,而不仅仅是最新记录。
获取具有特定状态原因的所有记录
User::whereStatusReason('User is active')->get();
注意:如果您启用了
keep_history
,它将返回所有具有该状态记录,而不仅仅是最新记录。
测试
composer test
更新日志
请参阅 更新日志 了解最近更改的更多信息。
贡献
请参阅 贡献指南 了解详细信息。
安全漏洞
请查阅 我们的安全策略 了解如何报告安全漏洞。
鸣谢
许可
MIT 许可证(MIT)。请参阅 许可文件 了解更多信息。