Almost every application has records that you can turn on and off. Products, users, integrations and feature flags are examples. Each time, we added an is_active column. Each time, we wrote the same scopes, the same methods and the same factory states.

We put this code into a small package: Laravel Activatable.

What the package does

Laravel Activatable gives an Eloquent model an active or an inactive state.

  • It gives methods to activate, deactivate and toggle the model.
  • It gives the scopes active() and inactive().
  • It sends an event when the state changes.
  • It can record when you deactivated the model.
  • It gives schema macros and factory states.

Install the package

composer require bernskiold/laravel-activatable

Add the column

Use the activatable() macro in your migration. The macro adds an is_active boolean column. The default value is true.

Schema::create('products', function (Blueprint $table) {
    $table->id();
    $table->string('name');
    $table->activatable();
    $table->timestamps();
});

Add the trait

Add the Activatable trait to the model. A new record is active by default.

use Bernskiold\LaravelActivatable\Concerns\Activatable;

class Product extends Model
{
    use Activatable;
}

Then change and read the state:

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

$product->isActive();   // true or false
$product->isInactive(); // true or false

Use the scopes to find the records:

Product::active()->get();
Product::inactive()->get();

When the model already has the requested state, activate() and deactivate() do nothing. They do not run a query, and they do not send an event.

Listen for changes

The package sends ModelActivated and ModelDeactivated. It sends an event only when the state changes. Each event has the model.

use Bernskiold\LaravelActivatable\Events\ModelDeactivated;

Event::listen(ModelDeactivated::class, function (ModelDeactivated $event) {
    // $event->model is now inactive
});

To change the state without events, use activateQuietly() and deactivateQuietly().

Record when a model was deactivated

Sometimes you must know when a record was turned off. Set $tracksInactivatedAt on the model. The package then sets inactivated_at when you deactivate the model. It clears the value when you activate the model.

class Product extends Model
{
    use Activatable;

    protected bool $tracksInactivatedAt = true;
}

Add the column with the inactivatedAt() macro. To use the timestamp on all models, set track_inactivated_at in the config.

Factory states

Add the ActivatableFactory trait to the factory. You then get the states active() and inactive(). If the model records inactivated_at, the states also set this value.

use Bernskiold\LaravelActivatable\Concerns\ActivatableFactory;

class ProductFactory extends Factory
{
    use ActivatableFactory;
}

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

A different column name

You can change the column name for all models in config/activatable.php. To change it for one model, use a constant:

class FeatureFlag extends Model
{
    use Activatable;

    public const ACTIVE_COLUMN = 'is_enabled';
}

A limit to know

The package sends events from activate() and deactivate(). If you set the column in a different way, for example with a mass update or with update(['is_active' => false]), the package does not send an event. Use the methods when listeners must know about the change.

Get the package

Laravel Activatable is open source, with the MIT license. The code and the full documentation are on GitHub. It works well together with Laravel Blame, our package that records who changed a model. If you find a problem or have an idea, open an issue. We are happy to hear from you.