mathewparet/model-suspension

将记录挂起(如用户挂起)和取消挂起功能添加到模型中。

v1.0.5 2023-08-03 04:44 UTC

This package is auto-updated.

Last update: 2024-09-03 07:11:31 UTC


README

为模型提供挂起的能力。

特性

  1. 挂起/取消挂起模型。
  2. 默认情况下,挂起的模型不会考虑任何查询(添加全局作用域)。

安装

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()模型将查询作用域限制为仅考虑挂起的记录。

事件

  1. suspending
  2. suspneded
  3. unsuspending
  4. unsuspended