digikraaft/ laravel-model-suspension
一个启用Eloquent模型挂起的包
Requires
- php: ^8.1
- laravel/framework: ^10.0|^11.0
Requires (Dev)
- orchestra/testbench: ^8.0|^9.0
- phpunit/phpunit: ^10.0|^11.0
- spatie/test-time: ^1.3
README
想象你想挂起一个Eloquent模型,比如用户模型。通常,向模型添加一个is_suspended
字段可以工作。但这样你就无法追踪模型为什么被挂起,挂起多少次,甚至无法设置挂起的时长。
此包提供了一个CanBeSuspended
特质,安装后即可实现所有这些功能。它可能看起来像这样
//suspend model
$model->suspend();
//suspend for the next 7 days
$model->suspend(7);
//suspend for the next 7 days with a reason
$model->suspend(7, 'privacy violation');
//get the latest suspension
$model->suspension(); //returns an instance of \Digikraaft\ModelSuspension\Suspension
//get the reason for suspension
$model->suspension()->reason; //returns 'privacy violation'
安装
您可以通过composer安装此包
composer require digikraaft/laravel-model-suspension
您必须使用以下命令发布迁移:
php artisan vendor:publish --provider="Digikraaft\ModelSuspension\ModelSuspensionServiceProvider" --tag="migrations"
运行迁移以发布suspensions
表,使用以下命令:
php artisan migrate
您可以可选地使用以下命令发布配置文件:
php artisan vendor:publish --provider="Digikraaft\ModelSuspension\ModelSuspensionServiceProvider" --tag="config"
将要发布到config/model-suspension.php
的文件内容
return [
/*
* The class name of the suspension model that holds all suspensions.
*
* The model must be or extend `Digikraaft\ModelSuspension\Suspension`.
*/
'suspension_model' => Digikraaft\ModelSuspension\Suspension::class,
/*
* The name of the column which holds the ID of the model related to the suspensions.
*
* Only change this value if you have set a different name in the migration for the suspensions table.
*/
'model_primary_key_attribute' => 'model_id',
];
用法
将CanBeSuspended
特质添加到您想要挂起的模型中
use Digikraaft\ModelSuspension\CanBeSuspended; use Illuminate\Database\Eloquent\Model; class EloquentModel extends Model { use CanBeSuspended; }
挂起模型
您可以使用以下方式挂起模型
$model->suspend();
可以用分别作为第一个和第二个参数的理由和挂起的天数
//suspend model for the next 7 days with a reason
$model->suspend(7, 'optional reason');
您还可以使用可选的第三个参数指定挂起的时长(以分钟或天为单位)
//suspend model for the next 30 minutes with a reason
$model->suspend(30, 'optional reason', Suspension::PERIOD_IN_MINUTES);
检索挂起
您可以使用以下方式获取模型的当前挂起状态
$model->suspension(); //returns the latest instance of Digikraaft\ModelSuspension\Suspension
可以使用以下方式检索模型的全部挂起状态
$model->suspensions();
可以使用allSuspensions
作用域检索模型的全部挂起状态
$allSuspensions = EloquentModel::allSuspensions();
可以使用activeSuspensions
作用域检索仅活跃的挂起状态
$allActiveSuspensions = EloquentModel::activeSuspensions();
可以使用nonActiveSuspensions
作用域检索仅非活跃的挂起状态
$allNonActiveSuspensions = EloquentModel::nonActiveSuspensions();
获取模型被挂起的次数
您可以使用以下方式获取模型被挂起的次数
$model->numberOfTimesSuspended();
要获取模型在某个时间段内被挂起的次数,请分别将格式为Carbon
的$from
和$to
日期作为第一个和第二个参数传递
//get the number of times a model has been suspended over the last month
$from = now()->subMonth();
$to = now();
$model->numberOfTimesSuspended($from, $to);
注意,如果$from
日期晚于$to
日期,将抛出InvalidDate
异常
检查模型是否被挂起
您可以使用以下方式检查模型是否当前被挂起
$model->isSuspended();
您还可以检查模型是否曾经被挂起
$model->hasEverBeenSuspended();
取消挂起模型
您可以通过使用unsuspend
方法在任何时候取消挂起模型
$model->unsuspend();
这将立即取消挂起模型。如果最初指定了挂起时长,它将被覆盖。
事件
当模型被挂起或取消挂起时,将触发Digikraaft\ModelSuspension\Events\ModelSuspensionChanged
事件。
namespace Digikraaft\ModelSuspension\Events;
use Digikraaft\ModelSuspension\Suspension;
use Illuminate\Database\Eloquent\Model;
class ModelSuspensionChanged
{
/** @var \Digikraaft\ModelSuspension\Suspension */
public Suspension $suspension;
/** @var \Illuminate\Database\Eloquent\Model */
public Model $model;
public function __construct(Model $model, Suspension $suspension)
{
$this->suspension = $suspension;
$this->model = $model;
}
}
自定义模型和迁移
您可以通过在model-suspension
配置文件的suspension_model
键中指定不同的类名来更改使用的模型。
您还可以在自定义迁移中使用时更改挂起表中使用的列名(默认为model_id
)。如果是这种情况,还必须更改model-suspension
配置文件中的model_primary_key_attribute
键。
测试
使用以下命令运行您的测试
composer test
更多精彩内容
请在此处查看更多免费精彩内容!
变更日志
请参阅变更日志获取最近更改的更多信息。
贡献
请参阅贡献指南获取详细信息。
安全
如果您发现任何与安全相关的问题,请通过电子邮件dev@digitalkraaft.com联系,而不是使用问题跟踪器。
致谢
许可协议
MIT 许可协议(MIT)。请参阅许可文件获取更多信息。