digikraaft/laravel-model-suspension
Composer 安装命令:
composer require digikraaft/laravel-model-suspension
包简介
A package to enable suspension of Eloquent Model
README 文档
README
Imagine you want to suspend an Eloquent model, a User model for example. Usually, adding
an is_suspended field to the model could work. But then you can't keep track of why the model was suspended,
how many times or even set the duration of the suspension.
This package provides a CanBeSuspended trait that enables you do all of these, once installed. It would be
something like this:
//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'
Installation
You can install the package via composer:
composer require digikraaft/laravel-model-suspension
You must publish the migration with:
php artisan vendor:publish --provider="Digikraaft\ModelSuspension\ModelSuspensionServiceProvider" --tag="migrations"
Run the migration to publish the suspensions table with:
php artisan migrate
You can optionally publish the config-file with:
php artisan vendor:publish --provider="Digikraaft\ModelSuspension\ModelSuspensionServiceProvider" --tag="config"
The content of the file that will be published to 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',
];
Usage
Add the CanBeSuspended trait to the model you would like to suspend:
use Digikraaft\ModelSuspension\CanBeSuspended; use Illuminate\Database\Eloquent\Model; class EloquentModel extends Model { use CanBeSuspended; }
Suspend model
You can suspend a model like this:
$model->suspend();
The number of days to be suspended for with a reason can be passed as the first and second arguments respectively:
//suspend model for the next 7 days with a reason
$model->suspend(7, 'optional reason');
You can also specify the suspension period (in minutes or days) with an optional third argument:
//suspend model for the next 30 minutes with a reason
$model->suspend(30, 'optional reason', Suspension::PERIOD_IN_MINUTES);
Retrieving suspensions
You can get the current suspension of the model like this:
$model->suspension(); //returns the latest instance of Digikraaft\ModelSuspension\Suspension
All associated suspensions of a model can be retrieved like this:
$model->suspensions();
The allSuspensions scope can be used to retrieve all the suspensions of the model:
$allSuspensions = EloquentModel::allSuspensions();
The activeSuspensions scope can be used to retrieve only active suspensions:
$allActiveSuspensions = EloquentModel::activeSuspensions();
The nonActiveSuspensions scope can be used to retrieve only non-active suspensions:
$allNonActiveSuspensions = EloquentModel::nonActiveSuspensions();
Get number of times a model has been suspended
You can get the number of times a model has been suspended like this:
$model->numberOfTimesSuspended();
To get the number of times a model has been suspended over a period,
pass in a Carbon formatted $from and $to dates as the first and second
arguments respectively:
//get the number of times a model has been suspended over the last month
$from = now()->subMonth();
$to = now();
$model->numberOfTimesSuspended($from, $to);
Note that an InvalidDate exception will be thrown if the $from date is later than the $to
Check if model has been suspended
You can check if a model is currently suspended:
$model->isSuspended();
You can also check if a model has ever been suspended:
$model->hasEverBeenSuspended();
Unsuspend model
You can unsuspend a model at any time by using the unsuspend method:
$model->unsuspend();
This will unsuspend the model immediately. If a suspension period has
been initially specified, it will be overridden.
Events
The Digikraaft\ModelSuspension\Events\ModelSuspensionChanged event will be dispatched when
a model is suspended or unsuspended.
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;
}
}
Custom model and migration
You can change the model used by specifying a different class name in the
suspension_model key of the model-suspension config file.
You can also change the column name used in the suspension table
(default is model_id) when using a custom migration. If this is the case,
also change the model_primary_key_attribute key of the model-suspension config file.
Testing
Use the command below to run your tests:
composer test
More Good Stuff
Check here for more awesome free stuff!
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email dev@digitalkraaft.com instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.
digikraaft/laravel-model-suspension 适用场景与选型建议
digikraaft/laravel-model-suspension 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 285 次下载、GitHub Stars 达 4, 最近一次更新时间为 2020 年 07 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「digikraaft」 「laravel-suspension」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 digikraaft/laravel-model-suspension 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 digikraaft/laravel-model-suspension 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 digikraaft/laravel-model-suspension 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
PHP Implementation of Mono API
PHP Implementation of Paystack API
A simple interface to Paystack's subscription billing services. It takes the pain of implementing subscription management off you.
PHP Implementation of abokiFX API
A tile to show list of Stripe Payments on Laravel Dashboard
A tile to show list of Stripe Customers on Laravel Dashboard
统计信息
- 总下载量: 285
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 18
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-07-24