mathewparet/requires-publishing

向模型添加发布和取消发布的记录功能。

v1.0.0 2023-08-03 05:58 UTC

This package is not auto-updated.

Last update: 2024-09-27 09:35:37 UTC


README

提供模型发布的能力。

特性

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

安装

composer require mathewparet/requires-publishing

在迁移中定义一个字段来存储发布状态

// define a 'published_at' field of type dateTime and is nullable
$table->requiresPublishing();

// define a 'active_at' field of type dateTime and is nullable
$table->requiresPublishing('active_at');

将此功能添加到您的模型中

// ...
use mathewparet\RequiresPublishing\RequiresPublishing;
// ...
class User extends Authenticatable
{
    // ...
    use RequiresPublishing;
    // ...

    /**
     * To use custom field name for publishing, use the below line. If the below line isn't 
     * defined, the field is assumed to be 'published_at'.
     */
    const PUBLISHED_AT = 'active_at';
}

API参考

此包引入了以下方法

名称可用性描述
requiresPublishing()蓝图宏,用于定义一个字段,用于存储记录的发布状态。
publish()模型标记模型为已发布。
publishQuietly()模型标记模型为已发布而不触发任何事件。
unpublish()模型、构建器标记模型为未发布。
unpublishQuietly()模型标记模型为未发布而不触发任何事件。
withUnpublished()模型将未发布的记录添加到作用域。
withoutUnpublished()模型从作用域中移除未发布的记录。
onlyUnpublished()模型将查询作用域限定为仅考虑未发布的记录。

事件

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