mathewparet/model-suspension

Add record suspension (like user suspension) and unsuspension features to a model.

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

This package is auto-updated.

Last update: 2024-05-03 06:31:11 UTC


README

Provide the ability to models to be suspended.

Features

  1. Suspend / Unsuspend a model.
  2. Suspended models are not considered for any queries by default (adds a global scope).

Installation

composer require mathewparet/model-suspension

Defining a field to hold suspension status in migrations

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

or

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

Add the funtionality to your model

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

This package introduces the below methods:

NameAvailabilityDescription
canBeSuspended()BlueprintMacro to define a field that holds the suspension state for the record.
suspend()ModelMark a model as suspended.
suspendQuetly()ModelMark a model as suspended without raising any events.
unsuspend()Model, BuilderMark a model as unsuspended.
unsuspendQuetly()ModelMark a model as unsuspended without raising any events.
withSuspended()ModelAdd suspended records to the scope.
withoutSuspended()ModelRemove suspended records from scope.
onlySuspended()ModelScope the query to only consider suspended records.

Events

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