PHPackages                             digikraaft/laravel-model-suspension - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. digikraaft/laravel-model-suspension

ActiveLibrary[Database &amp; ORM](/categories/database)

digikraaft/laravel-model-suspension
===================================

A package to enable suspension of Eloquent Model

v3.2.0(7mo ago)4194—0%1MITPHPPHP ^8.2CI passing

Since Jul 24Pushed 7mo ago1 watchersCompare

[ Source](https://github.com/digikraaft/laravel-model-suspension)[ Packagist](https://packagist.org/packages/digikraaft/laravel-model-suspension)[ Docs](https://github.com/digikraaft/laravel-model-suspension)[ RSS](/packages/digikraaft-laravel-model-suspension/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (4)Versions (12)Used By (0)

Suspend Eloquent models
=======================

[](#suspend-eloquent-models)

[![tests](https://github.com/digikraaft/laravel-model-suspension/workflows/tests/badge.svg)](https://github.com/digikraaft/laravel-model-suspension/workflows/tests/badge.svg)[![Build Status](https://camo.githubusercontent.com/7c11236f3254e927b499d666397d25516cbc29e53bf251c24fb054a6eff8449c/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f646967696b72616166742f6c61726176656c2d6d6f64656c2d73757370656e73696f6e2f6261646765732f6275696c642e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/digikraaft/laravel-model-suspension/build-status/master)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/aefa26f22c8633fb01481b6045fec2ea6007ab4f2331f633130d9f1a2172cb76/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f646967696b72616166742f6c61726176656c2d6d6f64656c2d73757370656e73696f6e2f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/digikraaft/laravel-model-suspension/?branch=master)[![Code Intelligence Status](https://camo.githubusercontent.com/4c89393f91fc243c5f602af52931a4fac2216216c5c92e6118815e7a9ac15328/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f646967696b72616166742f6c61726176656c2d6d6f64656c2d73757370656e73696f6e2f6261646765732f636f64652d696e74656c6c6967656e63652e7376673f623d6d6173746572)](https://scrutinizer-ci.com/code-intelligence)[![License: MIT](https://camo.githubusercontent.com/784362b26e4b3546254f1893e778ba64616e362bd6ac791991d2c9e880a3a64e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c6963656e73652d4d49542d677265656e2e737667)](https://opensource.org/licenses/MIT)

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:

```
//suspend model
$model->suspend();

//suspend for the next 7 days
$model->suspend(7);

//suspend for the next 7 days with a reason
$model->suspend(7, 'privacy violation');

//get the latest suspension
$model->suspension(); //returns an instance of \Digikraaft\ModelSuspension\Suspension

//get the reason for suspension
$model->suspension()->reason; //returns 'privacy violation'

```

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require digikraaft/laravel-model-suspension
```

You must publish the migration with:

```
php artisan vendor:publish --provider="Digikraaft\ModelSuspension\ModelSuspensionServiceProvider" --tag="migrations"
```

Run the migration to publish the `suspensions` table with:

```
php artisan migrate

```

You can optionally publish the config-file with:

```
php artisan vendor:publish --provider="Digikraaft\ModelSuspension\ModelSuspensionServiceProvider" --tag="config"

```

The content of the file that will be published to `config/model-suspension.php`:

```
return [
    /*
      * The class name of the suspension model that holds all suspensions.
      *
      * The model must be or extend `Digikraaft\ModelSuspension\Suspension`.
      */
    'suspension_model' => Digikraaft\ModelSuspension\Suspension::class,

    /*
     * The name of the column which holds the ID of the model related to the suspensions.
     *
     * Only change this value if you have set a different name in the migration for the suspensions table.
     */
    'model_primary_key_attribute' => 'model_id',

];

```

Usage
-----

[](#usage)

Add the `CanBeSuspended` trait to the model you would like to suspend:

```
use Digikraaft\ModelSuspension\CanBeSuspended;
use Illuminate\Database\Eloquent\Model;

class EloquentModel extends Model
{
    use CanBeSuspended;
}
```

### Suspend model

[](#suspend-model)

You can suspend a model like this:

```
$model->suspend();

```

The number of days to be suspended for with a reason can be passed as the first and second arguments respectively:

```
//suspend model for the next 7 days with a reason
$model->suspend(7, 'optional reason');

```

You can also specify the suspension period (in minutes or days) with an optional third argument:

```
//suspend model for the next 30 minutes with a reason
$model->suspend(30, 'optional reason', Suspension::PERIOD_IN_MINUTES);

```

### Retrieving suspensions

[](#retrieving-suspensions)

You can get the current suspension of the model like this:

```
$model->suspension(); //returns the latest instance of Digikraaft\ModelSuspension\Suspension

```

All associated suspensions of a model can be retrieved like this:

```
$model->suspensions();

```

The `allSuspensions` scope can be used to retrieve all the suspensions of the model:

```
$allSuspensions = EloquentModel::allSuspensions();

```

The `activeSuspensions` scope can be used to retrieve only active suspensions:

```
$allActiveSuspensions = EloquentModel::activeSuspensions();

```

The `nonActiveSuspensions` scope can be used to retrieve only non-active suspensions:

```
$allNonActiveSuspensions = EloquentModel::nonActiveSuspensions();

```

### Get number of times a model has been suspended

[](#get-number-of-times-a-model-has-been-suspended)

You can get the number of times a model has been suspended like this:

```
$model->numberOfTimesSuspended();

```

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:

```
//get the number of times a model has been suspended over the last month

$from = now()->subMonth();
$to = now();

$model->numberOfTimesSuspended($from, $to);

```

Note that an `InvalidDate` exception will be thrown if the `$from` date is later than the `$to`

### Check if model has been suspended

[](#check-if-model-has-been-suspended)

You can check if a model is currently suspended:

```
$model->isSuspended();

```

You can also check if a model has ever been suspended:

```
$model->hasEverBeenSuspended();

```

### Unsuspend model

[](#unsuspend-model)

You can unsuspend a model at any time by using the `unsuspend` method:

```
$model->unsuspend();

```

This will `unsuspend` the model immediately. If a suspension period has been initially specified, it will be overridden.

### Events

[](#events)

The `Digikraaft\ModelSuspension\Events\ModelSuspensionChanged` event will be dispatched when a model is suspended or unsuspended.

```
namespace Digikraaft\ModelSuspension\Events;

use Digikraaft\ModelSuspension\Suspension;
use Illuminate\Database\Eloquent\Model;

class ModelSuspensionChanged
{
    /** @var \Digikraaft\ModelSuspension\Suspension */
    public Suspension $suspension;

    /** @var \Illuminate\Database\Eloquent\Model */
    public Model $model;

    public function __construct(Model $model, Suspension $suspension)
    {
        $this->suspension = $suspension;
        $this->model = $model;
    }
}

```

### Custom model and migration

[](#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
-------

[](#testing)

Use the command below to run your tests:

```
composer test
```

More Good Stuff
---------------

[](#more-good-stuff)

Check [here](https://github.com/digikraaft) for more awesome free stuff!

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

Security
--------

[](#security)

If you discover any security related issues, please email  instead of using the issue tracker.

Credits
-------

[](#credits)

- [Tim Oladoyinbo](https://github.com/timoladoyinbo)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

45

—

FairBetter than 92% of packages

Maintenance62

Regular maintenance activity

Popularity19

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity74

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~208 days

Recently: every ~366 days

Total

10

Last Release

238d ago

Major Versions

v1.2.0 → v2.0.02021-09-13

v2.1.0 → v3.0.02023-03-13

PHP version history (5 changes)v1.0.0PHP ^7.4

v2.0.0PHP ^8.0

v2.1.0PHP ^8.0|^8.1

v3.0.0PHP ^8.1

v3.2.0PHP ^8.2

### Community

Maintainers

![](https://www.gravatar.com/avatar/298ea582e7fdad689b46e4fa771f3eb5caa963a9a7a76b2416dd98bada23d15f?d=identicon)[digikraaft](/maintainers/digikraaft)

---

Top Contributors

[![timoladoyinbo](https://avatars.githubusercontent.com/u/66416657?v=4)](https://github.com/timoladoyinbo "timoladoyinbo (37 commits)")

---

Tags

digikraafteloquentlaravelsuspendsuspensiondigikraaftlaravel-suspension

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/digikraaft-laravel-model-suspension/health.svg)

```
[![Health](https://phpackages.com/badges/digikraaft-laravel-model-suspension/health.svg)](https://phpackages.com/packages/digikraaft-laravel-model-suspension)
```

###  Alternatives

[anourvalar/eloquent-serialize

Laravel Query Builder (Eloquent) serialization

11320.2M21](/packages/anourvalar-eloquent-serialize)[overtrue/laravel-versionable

Make Laravel model versionable.

585308.0k5](/packages/overtrue-laravel-versionable)[abbasudo/laravel-purity

elegant way to add filter and sort in laravel

514330.5k1](/packages/abbasudo-laravel-purity)[statamic-rad-pack/runway

Eloquently manage your database models in Statamic.

135192.6k5](/packages/statamic-rad-pack-runway)[dragon-code/laravel-deploy-operations

Performing any actions during the deployment process

240173.5k2](/packages/dragon-code-laravel-deploy-operations)[stayallive/laravel-eloquent-observable

Register Eloquent model event listeners just-in-time directly from the model.

2928.9k7](/packages/stayallive-laravel-eloquent-observable)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
