mathewparet / model-suspension
Add record suspension (like user suspension) and unsuspension features to a model.
v1.0.9
2025-01-05 12:55 UTC
Requires
- php: ^8.0
README
Provide the ability to models to be suspended.
Features
- Suspend / Unsuspend a model.
- 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:
Name | Availability | Description |
---|---|---|
canBeSuspended() | Blueprint | Macro to define a field that holds the suspension state for the record. |
suspend() | Model | Mark a model as suspended. |
suspendQuetly() | Model | Mark a model as suspended without raising any events. |
unsuspend() | Model, Builder | Mark a model as unsuspended. |
unsuspendQuetly() | Model | Mark a model as unsuspended without raising any events. |
withSuspended() | Model | Add suspended records to the scope. |
withoutSuspended() | Model | Remove suspended records from scope. |
onlySuspended() | Model | Scope the query to only consider suspended records. |
Events
suspending
suspneded
unsuspending
unsuspended