PHPackages                             jxckaroo/laravel-state-machine - 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. jxckaroo/laravel-state-machine

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

jxckaroo/laravel-state-machine
==============================

A simple state machine to handle model transactions, based on a list of pre-defined rules, for Laravel.

v1.1.0(4y ago)19251MITPHPPHP ^7.4|^8.0

Since Jan 31Pushed 3y ago1 watchersCompare

[ Source](https://github.com/Jxckaroo/laravel-state-machine)[ Packagist](https://packagist.org/packages/jxckaroo/laravel-state-machine)[ Docs](https://github.com/jxckaroo/laravel-state-machine)[ RSS](/packages/jxckaroo-laravel-state-machine/feed)WikiDiscussions develop Synced today

READMEChangelog (2)Dependencies (9)Versions (5)Used By (0)

[![](./banner.png)](./banner.png)

[![Latest Version on Packagist](https://camo.githubusercontent.com/40b3d4d167ad532ff46d0c752910bba55a65c3abbb04952a317ec868e47625f8/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6a78636b61726f6f2f6c61726176656c2d73746174652d6d616368696e652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/jxckaroo/laravel-state-machine)[![Total Downloads](https://camo.githubusercontent.com/4adf430188447ca7947bf22de62bcb0b4234c5e7b0d6fa9a62cff409693240d1/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6a78636b61726f6f2f6c61726176656c2d73746174652d6d616368696e652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/jxckaroo/laravel-state-machine)[![GitHub Actions](https://github.com/Jxckaroo/laravel-state-machine/actions/workflows/php-cs-fixer.yml/badge.svg)](https://github.com/Jxckaroo/laravel-state-machine/actions/workflows/php-cs-fixer.yml/badge.svg)[![GitHub Actions](https://github.com/Jxckaroo/laravel-state-machine/actions/workflows/psalm.yml/badge.svg)](https://github.com/Jxckaroo/laravel-state-machine/actions/workflows/psalm.yml/badge.svg)[![GitHub Actions](https://github.com/Jxckaroo/laravel-state-machine/actions/workflows/run-tests.yml/badge.svg)](https://github.com/Jxckaroo/laravel-state-machine/actions/workflows/run-tests.yml/badge.svg)

A simple state machine that allows transitioning model states based on pre-defined rules.

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

[](#installation)

You can install the package via composer:

```
composer require jxckaroo/laravel-state-machine
```

Run the package migrations:

```
php artisan migrate
```

Usage
-----

[](#usage)

Add the `Jxckaroo\StateMachine\Stateable` trait to your model(s) that require state management &amp; define your states on your model(s):

### States &amp; Rules

[](#states--rules)

Below is an example of a ready-to-go model.

```
use Illuminate\Database\Eloquent\Model;
use Jxckaroo\StateMachine\Stateable;

class Order extends Model
{
    use Stateable;

    protected array $states = [
        'factory' => ExampleRule::class,
        'complete' => [
            ExampleRule::class,
            ExampleRuleFalse::class
        ]
    ];

    // ...
}
```

When defining your states, you can specify either one or many rules as above. Your rules must extend `Jxckaroo\StateMachine\Contracts\StateRule` and must contain a `validate` method as per the below example:

```
class ExampleRule extends StateRule
{
    public function validate(Model $model): bool
    {
        return $model->getKey() !== null;
    }
}
```

You can also add specific error messages when a validation fails, like so:

```
class ExampleRuleFailure extends StateRule
{
    public function validate(Model $model): bool
    {
        if (!$model->isProductionReady()) {
            $this->addError("Model is not ready for production.");
            return false;
        }

        return true;
    }
}
```

These error messages can then be retrieved by calling `$model->stateErrors()` after attempting to transition state, resulting in a collection of errors returning, like below:

```
Illuminate\Support\Collection Object
(
    [items:protected] => Array
        (
            [0] => Array
                (
                    [message] => Model is not ready for production.
                    [rule] => Jxckaroo\StateMachine\Rules\ExampleRuleFailure
                )

        )

    [escapeWhenCastingToString:protected] =>
)
```

### Model Interactions

[](#model-interactions)

```
// Get model
$order = Order::find(1);

// Get current state of a model
$order->state;

// Get the state history of a model
$order->stateHistory;

// Attempt to transition model to one of your defined states
$order->transitionToState("factory");

// Attempt to transition model to previous state as defined in $order->states
$order->transitionToPreviousState();

// Attempt to transition model to next state as defined in $order->states
$order->transitionToNextState();

// Get all available states on model
$order->states();
```

Check if a state transition is successful:

```
$order = Order::find(1);

if ($order->transitionToState("complete")->isSuccessful()) {
    // Successful transition
} else {
    // Get a collection of all rules that failed
    $order->stateErrors();
}
```

### Testing

[](#testing)

```
composer test
```

### Changelog

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information 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)

- [Jack Mollart](https://github.com/jxckaroo)
- [All Contributors](../../contributors)

License
-------

[](#license)

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

Laravel Package Boilerplate
---------------------------

[](#laravel-package-boilerplate)

This package was generated using the [Laravel Package Boilerplate](https://laravelpackageboilerplate.com).

###  Health Score

28

—

LowBetter than 52% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity16

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

 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 ~0 days

Total

2

Last Release

1613d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/2db3f41efda8aa51b226657fea51aa7102a40d117f197a82e9dfb3e08974deae?d=identicon)[Jxckaroo](/maintainers/Jxckaroo)

---

Top Contributors

[![Jxckaroo](https://avatars.githubusercontent.com/u/24681027?v=4)](https://github.com/Jxckaroo "Jxckaroo (71 commits)")

---

Tags

composerlaravellaravel-packagephpstate-machinelaravelstate-machinejxckaroolaravel-state-machine

###  Code Quality

TestsPHPUnit

Static AnalysisPsalm

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/jxckaroo-laravel-state-machine/health.svg)

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

###  Alternatives

[psalm/plugin-laravel

Psalm plugin for Laravel

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

The official AI SDK for Laravel.

1.0k3.2M194](/packages/laravel-ai)[yajra/laravel-oci8

Oracle DB driver for Laravel via OCI8

8793.2M25](/packages/yajra-laravel-oci8)[api-platform/laravel

API Platform support for Laravel

58171.5k14](/packages/api-platform-laravel)[wearepixel/laravel-cart

A cart implementation for Laravel

1374.8k](/packages/wearepixel-laravel-cart)

PHPackages © 2026

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