Download the PHP package digikraaft/laravel-model-suspension without Composer
On this page you can find all versions of the php package digikraaft/laravel-model-suspension. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download digikraaft/laravel-model-suspension
More information about digikraaft/laravel-model-suspension
Files in digikraaft/laravel-model-suspension
Package laravel-model-suspension
Short Description A package to enable suspension of Eloquent Model
License MIT
Homepage https://github.com/digikraaft/laravel-model-suspension
Informations about the package laravel-model-suspension
Suspend Eloquent models
Imagine you want to suspend an Eloquent model, a User model for example. Usually, adding
an is_suspended
field to the model could work. But then you can't keep track of why the model was suspended,
how many times or even set the duration of the suspension.
This package provides a CanBeSuspended
trait that enables you do all of these, once installed. It would be
something like this:
Installation
You can install the package via composer:
You must publish the migration with:
Run the migration to publish the suspensions
table with:
You can optionally publish the config-file with:
The content of the file that will be published to config/model-suspension.php
:
Usage
Add the CanBeSuspended
trait to the model you would like to suspend:
Suspend model
You can suspend a model like this:
The number of days to be suspended for with a reason can be passed as the first and second arguments respectively:
You can also specify the suspension period (in minutes or days) with an optional third argument:
Retrieving suspensions
You can get the current suspension of the model like this:
All associated suspensions of a model can be retrieved like this:
The allSuspensions
scope can be used to retrieve all the suspensions of the model:
The activeSuspensions
scope can be used to retrieve only active suspensions:
The nonActiveSuspensions
scope can be used to retrieve only non-active suspensions:
Get number of times a model has been suspended
You can get the number of times a model has been suspended like this:
To get the number of times a model has been suspended over a period,
pass in a Carbon
formatted $from
and $to
dates as the first and second
arguments respectively:
Note that an InvalidDate
exception will be thrown if the $from
date is later than the $to
Check if model has been suspended
You can check if a model is currently suspended:
You can also check if a model has ever been suspended:
Unsuspend model
You can unsuspend a model at any time by using the unsuspend
method:
This will unsuspend
the model immediately. If a suspension period has
been initially specified, it will be overridden.
Events
The Digikraaft\ModelSuspension\Events\ModelSuspensionChanged
event will be dispatched when
a model is suspended or unsuspended.
Custom model and migration
You can change the model used by specifying a different class name in the
suspension_model
key of the model-suspension
config file.
You can also change the column name used in the suspension table
(default is model_id
) when using a custom migration. If this is the case,
also change the model_primary_key_attribute
key of the model-suspension
config file.
Testing
Use the command below to run your tests:
More Good Stuff
Check here for more awesome free stuff!
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
Credits
- Tim Oladoyinbo
- All Contributors
License
The MIT License (MIT). Please see License File for more information.