PHPackages                             bernskiold/laravel-activatable - 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. bernskiold/laravel-activatable

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

bernskiold/laravel-activatable
==============================

Give Eloquent models an active/inactive state with scopes, events, factory states and a schema macro.

1.0.0(1mo ago)066↓25%[1 PRs](https://github.com/bernskiold/laravel-activatable/pulls)MITPHPPHP ^8.2CI passing

Since Jun 6Pushed 1mo agoCompare

[ Source](https://github.com/bernskiold/laravel-activatable)[ Packagist](https://packagist.org/packages/bernskiold/laravel-activatable)[ Docs](https://github.com/bernskiold/laravel-activatable)[ RSS](/packages/bernskiold-laravel-activatable/feed)WikiDiscussions main Synced 1w ago

READMEChangelog (1)Dependencies (9)Versions (3)Used By (0)

Activate and deactivate Eloquent models, the easy way
=====================================================

[](#activate-and-deactivate-eloquent-models-the-easy-way)

[![Latest Version on Packagist](https://camo.githubusercontent.com/dabdb83b71a999cf761af37142a6868007ef55d6e98011bd10ee91c42ca24ca5/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6265726e736b696f6c642f6c61726176656c2d6163746976617461626c652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/bernskiold/laravel-activatable)[![GitHub Tests Action Status](https://camo.githubusercontent.com/df1aa7867b9f91a74a6f95a3cfaafa83b63a6c4cf9db64c5ed99d9645cf94e11/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6265726e736b696f6c642f6c61726176656c2d6163746976617461626c652f72756e2d74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/bernskiold/laravel-activatable/actions?query=workflow%3Arun-tests+branch%3Amain)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/81e0ed1b0a2141dc97f00a4ad073e436f3cec29731f76a488cfa298ea565a929/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6265726e736b696f6c642f6c61726176656c2d6163746976617461626c652f6669782d7068702d636f64652d7374796c652d6973737565732e796d6c3f6272616e63683d6d61696e266c6162656c3d636f64652532307374796c65267374796c653d666c61742d737175617265)](https://github.com/bernskiold/laravel-activatable/actions?query=workflow%3A%22Fix+PHP+code+style+issues%22+branch%3Amain)[![Total Downloads](https://camo.githubusercontent.com/2ddcfb613657c46e8189f92cb53309c03eeaa3b9ffb19a44722148386bbc2586/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6265726e736b696f6c642f6c61726176656c2d6163746976617461626c652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/bernskiold/laravel-activatable)

Almost every app has models that can be turned on and off — users, products, feature flags, integrations. This package gives those models a clean active/inactive state, so you stop re-writing the same `is_active` boolean, the same scopes, and the same factory states in every project.

```
$product->activate();
$product->deactivate();

Product::active()->get();   // only the live ones
Product::inactive()->get(); // only the switched-off ones
```

Add one trait, add one column, and you're done. Everything else — scopes, events, factory states, an optional "when was this turned off?" timestamp — is there when you want it and out of the way when you don't.

Why you'll like it
------------------

[](#why-youll-like-it)

- **One trait, zero ceremony.** New records default to active, the column is cast to a boolean, and you get `activate()` / `deactivate()` / `toggleActive()` for free.
- **Readable scopes.** `Product::active()` and `Product::inactive()` read like English, and invert with a single argument.
- **Idempotent by design.** Calling `deactivate()` on an already-inactive model is a true no-op — no redundant write, no misleading event.
- **Events when it matters.** `ModelActivated` and `ModelDeactivated` fire only when the state actually changes, so listeners aren't spammed.
- **Optional deactivation timestamp.** Opt in to an `inactivated_at` column and the package keeps it in sync — set on deactivation, cleared on activation.
- **Tidy migrations and factories.** A `$table->activatable()` schema macro and `active()` / `inactive()` factory states keep your test and migration code expressive.

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

[](#installation)

You can install the package via Composer:

```
composer require bernskiold/laravel-activatable
```

If you'd like to change the column name, the default state, or enable the deactivation timestamp globally, publish the config:

```
php artisan vendor:publish --tag=activatable-config
```

Schema
------

[](#schema)

Add the boolean column with the `activatable()` blueprint macro:

```
Schema::create('products', function (Blueprint $table) {
    $table->id();
    $table->string('name');
    $table->activatable();   // adds an `is_active` boolean, default true
    $table->timestamps();
});
```

Both schema macros return the column definition, so you can keep chaining:

```
$table->activatable()->comment('Whether the product is purchasable.');
```

Usage
-----

[](#usage)

Add the trait to your model:

```
use Bernskiold\LaravelActivatable\Concerns\Activatable;

class Product extends Model
{
    use Activatable;
}
```

New records are active by default. From there:

```
$product->isActive();    // bool
$product->isInactive();  // bool

$product->activate();      // sets active, dispatches ModelActivated (on change)
$product->deactivate();    // sets inactive, dispatches ModelDeactivated (on change)
$product->toggleActive();  // flips it

Product::active()->get();      // only active
Product::inactive()->get();    // only inactive
Product::active(false)->get(); // inverted — same as inactive()
```

Calling `activate()` / `deactivate()` on a model that is already in the requested state does nothing at all — no query is run and no event is dispatched.

Need to change the state without firing events (and without touching the model's own `saving`/`saved` events)? Reach for the quiet variants:

```
$product->activateQuietly();
$product->deactivateQuietly();
```

### Events

[](#events)

`activate()` and `deactivate()` dispatch events **only when the state actually changes**. Each event exposes the `$model` that changed:

```
use Bernskiold\LaravelActivatable\Events\ModelDeactivated;

Event::listen(ModelDeactivated::class, function (ModelDeactivated $event) {
    // $event->model was just switched off
});
```

### Tracking when a model was deactivated

[](#tracking-when-a-model-was-deactivated)

Sometimes "is it off?" isn't enough — you want to know *when* it was switched off. Opt a model into an `inactivated_at` timestamp and the package keeps it in sync: it's set on deactivation and cleared again on activation.

```
class Product extends Model
{
    use Activatable;

    protected bool $tracksInactivatedAt = true;
}
```

Add the column with the `inactivatedAt()` macro (or enable it for every model via the `track_inactivated_at` config option):

```
$table->activatable();
$table->inactivatedAt();   // adds a nullable `inactivated_at` timestamp
```

### Factory states

[](#factory-states)

Add the factory trait for expressive `active()` / `inactive()` states. They respect the model's column (and the deactivation timestamp, if it tracks one):

```
use Bernskiold\LaravelActivatable\Concerns\ActivatableFactory;

class ProductFactory extends Factory
{
    use ActivatableFactory;
}

Product::factory()->inactive()->create();
```

### A different column name

[](#a-different-column-name)

Override the column globally in `config/activatable.php`, or per model with a constant:

```
class FeatureFlag extends Model
{
    use Activatable;

    public const ACTIVE_COLUMN = 'is_enabled';
}
```

### Type-hinting

[](#type-hinting)

The trait implements the `Bernskiold\LaravelActivatable\Contracts\Activatable` interface, so you can type-hint against any activatable model:

```
use Bernskiold\LaravelActivatable\Contracts\Activatable;

function publish(Activatable $model): void
{
    $model->activate();
}
```

Testing
-------

[](#testing)

```
composer test
```

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 Vulnerabilities
------------------------

[](#security-vulnerabilities)

Please review [our security policy](SECURITY.md) on how to report security vulnerabilities.

Credits
-------

[](#credits)

- [Erik Bernskiöld](https://bernskiold.com)
- [All Contributors](../../contributors)

License
-------

[](#license)

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

###  Health Score

42

—

FairBetter than 88% of packages

Maintenance92

Actively maintained with recent releases

Popularity12

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity47

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 87.5% 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

Unknown

Total

1

Last Release

48d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/8409819?v=4)[Bernskiold](/maintainers/bernskiold)[@bernskiold](https://github.com/bernskiold)

---

Top Contributors

[![ErikBernskiold](https://avatars.githubusercontent.com/u/1166728?v=4)](https://github.com/ErikBernskiold "ErikBernskiold (7 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (1 commits)")

---

Tags

activatableactiveeloquentlaravellaravel-packagephplaraveleloquentstatusactivebernskioldactivatable

###  Code Quality

TestsPest

Static AnalysisPHPStan

Code StyleLaravel Pint

Type Coverage Yes

### Embed Badge

![Health badge](/badges/bernskiold-laravel-activatable/health.svg)

```
[![Health](https://phpackages.com/badges/bernskiold-laravel-activatable/health.svg)](https://phpackages.com/packages/bernskiold-laravel-activatable)
```

###  Alternatives

[psalm/plugin-laravel

Psalm plugin for Laravel

3345.3M347](/packages/psalm-plugin-laravel)[laravel/ai

The official AI SDK for Laravel.

1.0k3.2M246](/packages/laravel-ai)[watson/validating

Eloquent model validating trait.

9803.5M55](/packages/watson-validating)[api-platform/laravel

API Platform support for Laravel

58174.6k17](/packages/api-platform-laravel)[reedware/laravel-relation-joins

Adds the ability to join on a relationship by name.

2111.3M18](/packages/reedware-laravel-relation-joins)[simplestats-io/laravel-client

Server-side analytics for Laravel that follows the full funnel from visit to registration to payment, attributed to the channel that drove it. Revenue, MRR, churn and ad-spend profit (ROAS/CAC) per channel. GDPR compliant, ad-blocker proof.

5022.6k](/packages/simplestats-io-laravel-client)

PHPackages © 2026

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