PHPackages                             dcodegroup/laravel-state-machines - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. dcodegroup/laravel-state-machines

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

dcodegroup/laravel-state-machines
=================================

Generates state machine classes for Laravel

1.1.0(1y ago)113.0k↓33.2%1MITPHPPHP ^8.2

Since Feb 20Pushed 1y ago3 watchersCompare

[ Source](https://github.com/dcodegroup/laravel-state-machines)[ Packagist](https://packagist.org/packages/dcodegroup/laravel-state-machines)[ Docs](https://github.com/dcodegroup/laravel-state-machines)[ RSS](/packages/dcodegroup-laravel-state-machines/feed)WikiDiscussions main Synced 3d ago

READMEChangelog (5)Dependencies (3)Versions (7)Used By (0)

State machines for Laravel
==========================

[](#state-machines-for-laravel)

This Laravel package simplifies the creation of state machines for your models, inspired by [Jake Bennett's talk](https://www.youtube.com/watch?v=1A1xFtlDyzU)at Laracon US Nashville in 2023.

Features
--------

[](#features)

- Database migrations for managing statuses and tracking status history.
- Artisan command to effortlessly generate state machines with states and transitions, for your specified model.

Installation &amp; setup
------------------------

[](#installation--setup)

You can install the package via composer:

```
composer require dcodegroup/laravel-state-machines

```

You can publish the migration files for statuses and statusables via the artisan command:

```
php artisan vendor:publish --tag=statuses-migrations

```

This command allows you to customize and adapt the migrations according to your specific needs.

Creating state machines
-----------------------

[](#creating-state-machines)

Run the `php artisan make:state-machine` command to create your first state machines. The command accepts a model as an argument, states and transitions as options.

```
php artisan make:state-machine User --states=Accepted,Pending,Rejected --transitions=approve,deny

```

This example will generate the following state machines under the `App/StateMachines/User` directory:

- UserStateContract.php which all transitions as methods.
- BaseUserState.php which implements the UserStateContract and automatically throws an exception for each transition.
- AcceptedState.php, PendingState.php, RejectedState.php which extend the BaseUserState.

You can also change the default namespace for your model if they are not stored under `App/Models` with the `--namespace`option. e.g. `--namespace=App\\ACL`.

Configuring statuses on a model.
--------------------------------

[](#configuring-statuses-on-a-model)

Add the `HasStates` trait to your model and add a `status_id` column to that same model. Implement the state method on your model, so it returns the user state contract.

```
use App\StateMachines\Users\AcceptedState;
use App\StateMachines\Users\PendingState;
use App\StateMachines\Users\RejectedState;
use http\Exception\InvalidArgumentException;

public function state()
{
    return match($this->status->machine_name) {
        'accepted' => new AcceptedState($this),
        'pending' => new PendingState($this),
        'rejected' => new RejectedState($this),
        'default' => throw new InvalidArgumentException('Invalid state'),
    }
}
```

Using state
-----------

[](#using-state)

Call the state method from you model and chain it with a transition method. e.g.

```
$user->state()->approve();
```

Default model state
-------------------

[](#default-model-state)

You can set a default state on your model by overriding the `$defaultState` property defined in the `HasStates` trait.

Assigning a state
-----------------

[](#assigning-a-state)

You can change the status of your model by using the `setStatus()` method which accepts a status machine name as an argument.

###  Health Score

37

—

LowBetter than 81% of packages

Maintenance37

Infrequent updates — may be unmaintained

Popularity28

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity59

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 80% 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 ~71 days

Total

5

Last Release

579d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/b0e8e7e3202508183e3ba9f9a95abcffdab114ce86e33e52a67abd58194d3d86?d=identicon)[dcodegroup](/maintainers/dcodegroup)

---

Top Contributors

[![yohannca](https://avatars.githubusercontent.com/u/7054076?v=4)](https://github.com/yohannca "yohannca (28 commits)")[![hungnt-dcode](https://avatars.githubusercontent.com/u/148025212?v=4)](https://github.com/hungnt-dcode "hungnt-dcode (3 commits)")[![dcodegroup](https://avatars.githubusercontent.com/u/11023162?v=4)](https://github.com/dcodegroup "dcodegroup (2 commits)")[![ntthanh1](https://avatars.githubusercontent.com/u/110796495?v=4)](https://github.com/ntthanh1 "ntthanh1 (2 commits)")

---

Tags

laravelstate-machines

### Embed Badge

![Health badge](/badges/dcodegroup-laravel-state-machines/health.svg)

```
[![Health](https://phpackages.com/badges/dcodegroup-laravel-state-machines/health.svg)](https://phpackages.com/packages/dcodegroup-laravel-state-machines)
```

###  Alternatives

[psalm/plugin-laravel

Psalm plugin for Laravel

3355.3M346](/packages/psalm-plugin-laravel)[laravel/ai

The official AI SDK for Laravel.

1.0k3.2M200](/packages/laravel-ai)[illuminate/queue

The Illuminate Queue package.

21332.6M1.6k](/packages/illuminate-queue)[mike-bronner/laravel-model-caching

Automatic caching for Eloquent models.

2.4k91.9k1](/packages/mike-bronner-laravel-model-caching)[fumeapp/modeltyper

Generate TypeScript interfaces from Laravel Models

198321.1k](/packages/fumeapp-modeltyper)[flarum/core

Delightfully simple forum software.

201.4M2.3k](/packages/flarum-core)

PHPackages © 2026

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