mathewparet / model-suspension
将记录挂起(如用户挂起)和取消挂起功能添加到模型中。
v1.0.5
2023-08-03 04:44 UTC
Requires
- php: ^8.0
- illuminate/contracts: ^10.0
- illuminate/database: ^8.0|^9.0|^10.0
- illuminate/support: ^10.0
This package is auto-updated.
Last update: 2024-09-03 07:11:31 UTC
README
为模型提供挂起的能力。
特性
- 挂起/取消挂起模型。
- 默认情况下,挂起的模型不会考虑任何查询(添加全局作用域)。
安装
composer require mathewparet/model-suspension
在迁移中定义一个字段来保存挂起状态
// define a 'suspended_at' field of type dateTime and is nullable
$table->canBeSuspended();
或者
// define a 'flagged_at' field of type dateTime and is nullable
$table->canBeSuspended('flagged_at');
将此功能添加到模型中
// ...
use mathewparet\ModelSuspension\CanBeSuspended;
// ...
class User extends Authenticatable
{
// ...
use CanBeSuspended;
// ...
/**
* To use custom field name for suspension, use the below line. If the below line isn't
* defined, the field is assumed to be 'suspended_at'.
*/
const SUSPENDED_AT = 'flagged_at';
}
API参考
此包引入了以下方法
名称 | 可用性 | 描述 |
---|---|---|
canBeSuspended() | 蓝图 | 宏,用于定义一个字段来保存记录的挂起状态。 |
suspend() | 模型 | 将模型标记为已挂起。 |
suspendQuetly() | 模型 | 将模型标记为已挂起,而不触发任何事件。 |
unsuspend() | 模型,构建器 | 将模型标记为未挂起。 |
unsuspendQuetly() | 模型 | 将模型标记为未挂起,而不触发任何事件。 |
withSuspended() | 模型 | 将挂起的记录添加到作用域中。 |
withoutSuspended() | 模型 | 从作用域中删除挂起的记录。 |
onlySuspended() | 模型 | 将查询作用域限制为仅考虑挂起的记录。 |
事件
suspending
suspneded
unsuspending
unsuspended