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

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

datomatic/laravel-enum-state-machine
====================================

A simple state machine for enums in Laravel

v0.3.1(3mo ago)21.5k—6.7%[1 PRs](https://github.com/datomatic/laravel-enum-state-machine/pulls)MITPHPPHP ^8.2CI passing

Since Nov 18Pushed 2mo ago1 watchersCompare

[ Source](https://github.com/datomatic/laravel-enum-state-machine)[ Packagist](https://packagist.org/packages/datomatic/laravel-enum-state-machine)[ Docs](https://github.com/datomatic/laravel-enum-state-machine)[ GitHub Sponsors](https://github.com/Datomatic)[ RSS](/packages/datomatic-laravel-enum-state-machine/feed)WikiDiscussions main Synced yesterday

READMEChangelog (6)Dependencies (26)Versions (10)Used By (0)

[![Enum Helper-Dark](branding/dark.png#gh-dark-mode-only)](branding/dark.png#gh-dark-mode-only)[![Enum Helper-Light](branding/light.png#gh-light-mode-only)](branding/light.png#gh-light-mode-only)

Laravel enum state machine
==========================

[](#laravel-enum-state-machine)

[![Latest Version on Packagist](https://camo.githubusercontent.com/402e5f7994da15c6abbc0fb365bc5b512ee2cd84bedfa0ff0279da2c9e28d44d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6461746f6d617469632f6c61726176656c2d656e756d2d73746174652d6d616368696e652e7376673f7374796c653d666f722d7468652d6261646765)](https://packagist.org/packages/datomatic/laravel-enum-state-machine)[![Pest Tests number](https://camo.githubusercontent.com/695c440cefd95abf3a15541060d6df52a955277e9450f272109b50476fd624ae/68747470733a2f2f696d672e736869656c64732e696f2f7374617469632f76313f6c6162656c3d2532337465737473266d6573736167653d383426636f6c6f723d464638384641267374796c653d666f722d7468652d6261646765266c6f676f3d646174613a696d6167652f706e673b6261736536342c6956424f5277304b47676f414141414e5355684555674141414277414141416343414d414141424630792b6d414141426956424d564555414141442f69507639795033586d2b6a2f6d502f2f7766566a36374a653662502f682f70567836703631373564353757517963662b69506e2f695072736e657a417264332b742f7170764e4a64364c502f6a5070753672762f6c50722f6b5070633537542f7276746335374e7036726a336f506c3337634c2f74666e2f77763964366272582f2f4c2f672f72596e2b6e2f677672576d2b6469364c582b6a5072736b6647577a4d707436626c6e3462646435374a6b364c575379636a2b76507175774e566f36726465366250376e7676596e75703931622f2b7666762f6c76746335374f71764e544673392f2f742f746435374c39742f722f69507064364c506170656a2f6f76703236627879363776396c666c64364c4a72344c6a777376622f7876332f6a7633397a76317436627547356354447265483569766c6335724a7936373656346378623537442f792f6835304d4f79344f4355786356613737582f69507065364c502f6a502b7075394c38742f2f2f7476755179636641724e7870364c7a41726431353172372f692f396e3462622f6a2f39653672542f69667237696672736b764c596e7568693837746738626c673762662f76762f2f6c502b77784e746a3962332f71762f2f6f502f2b69767a2f6c2f7238696672796e2f66766c505466705044656f66444b74756a48744f5758314e462f3473654333635238327346753763426f354c694d77504d724141414157485253546c4d412f577638464149432f644d452f576a2b337445472f507637393847316f48526a53306b314c4273574467734a2f7633362b66547938657a6e342b4c683239584e7a4d7a4c79734c41774c53777236366f704a71616b592b4e69344a37656e643062476c7059313158553034384b69636d49523866697a6c2b767741414156644a524546554b4d39747a325658416c4551674f4642425552706b453637753775374531594659516c31536276726c7a7444694c7673732b66632f6643656d584d76414568684b71553735395031724c6f7855445579456839665048307a37414c69567245592b53534e74784e533275706f75597637684f4c313931614b56735a48555467626e51505167446b7134637448646f516d54576d573457467a6c56554456704e4b58663266577057625a497755712f68636d6a5747596e536131705a5a6a456f6f6d72456456416973443743583647456234307271544f4478436a32314f6a444f766a5256386c326a68756442446368672f46556244495a4349547a775179483661392b32414d44626d3947666c74466e786741647451575567514a6c345651713337755063536e7366595a7a61763645773138665134665559504d37516e34755369496479783573614a36542b2f332b354b536c7473686963774932557066414b452f616f415254766e6e374b4d594d646c41557957525379484e324a65553432486c43693454737a5448636d756a33694d566450354a7a6f7941574e7a693654335a47724d46436c694b334241715253432f42322b3649787659594e634f2b324e7066762b79466931304c66425541414141415355564f524b35435949493d)](https://github.com/datomatic/laravel-enum-state-machine/tree/main/tests)[![GitHub Tests Action Status](https://camo.githubusercontent.com/504b9a6029dbda026ed84301e11d61dc664283eeba89d3d1d46f5808d30963cf/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6461746f6d617469632f6c61726176656c2d656e756d2d73746174652d6d616368696e652f72756e2d74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d746573747326636f6c6f723d354645384233267374796c653d666f722d7468652d6261646765266c6f676f3d646174613a696d6167652f706e673b6261736536342c6956424f5277304b47676f414141414e5355684555674141414277414141416343414d414141424630792b6d414141426956424d564555414141442f69507639795033586d2b6a2f6d502f2f7766566a36374a653662502f682f70567836703631373564353757517963662b69506e2f695072736e657a417264332b742f7170764e4a64364c502f6a5070753672762f6c50722f6b5070633537542f7276746335374e7036726a336f506c3337634c2f74666e2f77763964366272582f2f4c2f672f72596e2b6e2f677672576d2b6469364c582b6a5072736b6647577a4d707436626c6e3462646435374a6b364c575379636a2b76507175774e566f36726465366250376e7676596e75703931622f2b7666762f6c76746335374f71764e544673392f2f742f746435374c39742f722f69507064364c506170656a2f6f76703236627879363776396c666c64364c4a72344c6a777376622f7876332f6a7633397a76317436627547356354447265483569766c6335724a7936373656346378623537442f792f6835304d4f79344f4355786356613737582f69507065364c502f6a502b7075394c38742f2f2f7476755179636641724e7870364c7a41726431353172372f692f396e3462622f6a2f39653672542f69667237696672736b764c596e7568693837746738626c673762662f76762f2f6c502b77784e746a3962332f71762f2f6f502f2b69767a2f6c2f7238696672796e2f66766c505466705044656f66444b74756a48744f5758314e462f3473654333635238327346753763426f354c694d77504d724141414157485253546c4d412f577638464149432f644d452f576a2b337445472f507637393847316f48526a53306b314c4273574467734a2f7633362b66547938657a6e342b4c683239584e7a4d7a4c79734c41774c53777236366f704a71616b592b4e69344a37656e643062476c7059313158553034384b69636d49523866697a6c2b767741414156644a524546554b4d39747a325658416c4551674f4642425552706b453637753775374531594659516c31536276726c7a7444694c7673732b66632f6643656d584d76414568684b71553735395031724c6f7855445579456839665048307a37414c69567245592b53534e74784e533275706f75597637684f4c313931614b56735a48555467626e51505167446b7134637448646f516d54576d573457467a6c56554456704e4b58663266577057625a497755712f68636d6a5747596e536131705a5a6a456f6f6d72456456416973443743583647456234307271544f4478436a32314f6a444f766a5256386c326a68756442446368672f46556244495a4349547a775179483661392b32414d44626d3947666c74466e786741647451575567514a6c345651713337755063536e7366595a7a61763645773138665134665559504d37516e34755369496479783573614a36542b2f332b354b536c7473686963774932557066414b452f616f415254766e6e374b4d594d646c41557957525379484e324a65553432486c43693454737a5448636d756a33694d566450354a7a6f7941574e7a693654335a47724d46436c694b334241715253432f42322b3649787659594e634f2b324e7066762b79466931304c66425541414141415355564f524b35435949493d)](https://github.com/datomatic/laravel-enum-state-machine/actions/workflows/run-tests.yml)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/8241d85d196695c33c8c0b5349c73c64a8a973dc990dd301dba7f1603d629234/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6461746f6d617469632f6c61726176656c2d656e756d2d73746174652d6d616368696e652f6669782d7068702d636f64652d7374796c652d6973737565732e796d6c3f6c6162656c3d636f64652532307374796c6526636f6c6f723d354645384233267374796c653d666f722d7468652d6261646765)](https://github.com/datomatic/laravel-enum-state-machine/actions/workflows/php-cs-fixer.yml)[![Total Downloads](https://camo.githubusercontent.com/3b4247990fde43109044967e9dfff43bcdb75dab9c2f581c65bdeac16d3f64e9/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6461746f6d617469632f6c61726176656c2d656e756d2d73746174652d6d616368696e652e7376673f7374796c653d666f722d7468652d6261646765)](https://packagist.org/packages/datomatic/laravel-enum-state-machine)

This package it's simple state transitions control for enums in Laravel, this is not an implementation of state machine pattern. Allowing you to prevent unlogically transition and also controlling the initial state of the enum fields on your models.

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

[](#installation)

Laravel 10+ and PHP 8.2+ are required.

You can install the package via composer:

```
composer require datomatic/laravel-enum-state-machine
```

You can publish and run the migrations with:

```
php artisan vendor:publish --tag="enum-state-machine-migrations"
php artisan migrate
```

You can publish the config file with:

```
php artisan vendor:publish --tag="enum-state-machine-config"
```

This is the contents of the published config file:

```
return [
/*
    |--------------------------------------------------------------------------
    | Soft Mode Configuration
    |--------------------------------------------------------------------------
    |
    | The 'soft_mode' configuration allows for handling errors without
    | interrupting the application's execution. When this option is enabled,
    | no exceptions are thrown during state transitions and logged instead.
    | This helps prevent unexpected crashes, ensuring
    | greater application resilience, especially in scenarios where a failure
    | in the state machine should not disrupt the main program flow.
    | You can configure this modality for each model casting
    |
    */
    'soft_mode' => env('LARAVEL_ENUM_STATE_MACHINE_SOFT_MODE', false),
];
```

### Using Laravel IDE Helper?

[](#using-laravel-ide-helper)

If you are using [Laravel IDE Helper](https://github.com/barryvdh/laravel-ide-helper), you need to run the following command:

```
php artisan vendor:publish --tag="enum-state-machine-ide-helper-hooks"
```

and add `LaravelEnumStateMachineModelIdeHelperHook::class` on `model_hooks` array in `config/ide-helper.php`

```
    'model_hooks' => [
        ...,
        LaravelEnumStateMachineModelIdeHelperHook::class,
    ],
```

Usage
-----

[](#usage)

Laravel enum state machine it's a simple state transitions control for enums in Laravel, this is not an implementation of state machine pattern.

In the default mode, if the transition is not allowed, an exception `StatusTransitionDenied` will be thrown. In the soft mode, if the transition is not allowed, an error message will be logged.

### Setting the model

[](#setting-the-model)

You need to define the casts in your model and the transition control function. The first param on the casting is the enum class and the optional second param is the soft mode modality (if no second param is passed, the default mode configured in config file is used). You can cast multiple fields if needed. The transition method name is composed by the enum field name (camelCase) + Transitions and serve to define whether a transition is allowed or not.

```
class TestModel extends Model
{
    //Laravel 10
    protected $casts = [
        'status' => AsEnumStateMachine::class.':'.StatusEnum::class,  // ',true' for soft mode
    ];

    //Laravel 11
    protected function casts(): array
    {
        return [
            'status' => AsEnumStateMachine::of(FieldEnum::class, false),
        ];
    }

    /**
     * This method name is composed by the enum field name (camelCase) + Transitions
     */
    public function statusTransitions(?StatusEnum $from, ?StatusEnum $to): bool
    {
        return match ($from) {
            null => true, // initial state permitted to all states
            StatusEnum::PUBLIC => match ($to) {
                StatusEnum::PRIVATE => true,
                StatusEnum::PROTECTED => true,
                null => false,
                default => false
            },
            StatusEnum::PROTECTED => match ($to) {
                StatusEnum::PRIVATE => true,
                StatusEnum::PUBLIC => false,
                default => false
            },
            StatusEnum::PRIVATE => false, //final state
            default => true
        };
    }
```

### Use the model

[](#use-the-model)

```
$model = new TestModel;
$model->status = StatusEnum::PUBLIC; // OK
$model->save();

$model = TestModel::find(1);
$model->status; // StatusEnum::PUBLIC
$model->status = StatusEnum::PRIVATE; // OK
$model->status = StatusEnum::PUBLIC; // thrown `StatusTransitionDenied`
```

Testing
-------

[](#testing)

```
composer test
```

Changelog
---------

[](#changelog)

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

Security Vulnerabilities
------------------------

[](#security-vulnerabilities)

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

Credits
-------

[](#credits)

- [Alberto Peripolli](https://github.com/trippo)
- [All Contributors](../../contributors)

License
-------

[](#license)

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

###  Health Score

43

—

FairBetter than 89% of packages

Maintenance83

Actively maintained with recent releases

Popularity23

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity47

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 71.1% 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 ~97 days

Recently: every ~121 days

Total

6

Last Release

108d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/497169?v=4)[Alberto Peripolli](/maintainers/trippo)[@trippo](https://github.com/trippo)

---

Top Contributors

[![trippo](https://avatars.githubusercontent.com/u/497169?v=4)](https://github.com/trippo "trippo (32 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (8 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (5 commits)")

---

Tags

laravelDatomaticlaravel-enum-state-machine

###  Code Quality

TestsPest

Static AnalysisPHPStan

Code StyleLaravel Pint

### Embed Badge

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

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

###  Alternatives

[spatie/laravel-pdf

Create PDFs in Laravel apps

1.0k4.8M47](/packages/spatie-laravel-pdf)[codewithdennis/filament-select-tree

The multi-level select field enables you to make single selections from a predefined list of options that are organized into multiple levels or depths.

329530.5k29](/packages/codewithdennis-filament-select-tree)[worksome/exchange

Check Exchange Rates for any currency in Laravel.

124603.0k](/packages/worksome-exchange)[rawilk/profile-filament-plugin

Profile &amp; MFA starter kit for filament.

3914.6k](/packages/rawilk-profile-filament-plugin)[tarfin-labs/event-machine

Event-driven state machines for Laravel with event sourcing, type-safe context, and full audit trail.

199.4k](/packages/tarfin-labs-event-machine)[tapp/filament-form-builder

User facing form builder using Filament components

132.4k3](/packages/tapp-filament-form-builder)

PHPackages © 2026

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