mathewparet/requires-publishing

Add record publishing unpublishing features to a model.

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

This package is not auto-updated.

Last update: 2024-05-24 07:55:55 UTC


README

Provide the ability to models to be published.

Features

  1. Publish / Unpublish a model.
  2. Published models are not considered for any queries by default (adds a global scope).

Installation

composer require mathewparet/requires-publishing

Defining a field to hold publish status in migrations

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

or

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

Add the funtionality to your model

// ...
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 referance

This package introduces the below methods:

NameAvailabilityDescription
requiresPublishing()BlueprintMacro to define a field that holds the publish state for the record.
publish()ModelMark a model as published.
publishQuetly()ModelMark a model as published without raising any events.
unpublish()Model, BuilderMark a model as unpublished.
unpublishQuetly()ModelMark a model as unpublished without raising any events.
withUnpublished()ModelAdd unpublished records to the scope.
withoutUnpublished()ModelRemove unpublished records from scope.
onlyUnpublished()ModelScope the query to only consider unpublished records.

Events

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