PHPackages                             bjuppa/eloquent-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. [Database &amp; ORM](/categories/database)
4. /
5. bjuppa/eloquent-state-machine

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

bjuppa/eloquent-state-machine
=============================

Finite state machines for Laravel Eloquent models

v0.1.0(2y ago)3100[2 issues](https://github.com/bjuppa/eloquent-state-machine/issues)MITPHP

Since Aug 31Pushed 2y ago1 watchersCompare

[ Source](https://github.com/bjuppa/eloquent-state-machine)[ Packagist](https://packagist.org/packages/bjuppa/eloquent-state-machine)[ Docs](https://github.com/bjuppa/eloquent-state-machine)[ RSS](/packages/bjuppa-eloquent-state-machine/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (1)Dependencies (4)Versions (2)Used By (0)

State machines for Laravel Eloquent models
==========================================

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

This package allows you to treat a Laravel Eloquent model and its relationships as [extended state](https://en.wikipedia.org/wiki/UML_state_machine#Extended_states)in a [hierarchical state machine](https://en.wikipedia.org/wiki/UML_state_machine#Hierarchically_nested_states).

The philosophy that sets this package apart from most other state machine implementations is that behavior is declared **in code** in classes representing *states* and *events*, and the *model* itself determines the current state. There is no overall configuration of the state machine's *graph*. The current state handles incoming events and *transitions* the model into another state.

- Every state and event is represented by a PHP class, extending a relevant base class.
- A state declares the *superstate* (*composite* or *root*) it belongs too.
- Events are dispatched to the current state of the model.
- Event handling happens within database transactions with pessimistic row-locks.
- The current state evaluates an incoming event (*guards*) and may initiate a *transition* to another (named) state.
- Events not explicitly handled by the current state will bubble up the state branch.
- States have *entry* and *exit* actions and events have *actions* that manipulate the model during a transition.
- Side effects can be deferred for processing to after the transition is completed.
- Any anomalies during transitions throws exceptions, triggering transaction rollback.

Project status
--------------

[](#project-status)

I, Björn Nilsved, created this package back in 2020 for a Laravel app that has been in production since 2021. The development process of the state machine functionality was rather exploratory and focused on the needs of that specific app. As the state machine API, base classes, etc were very much in flux, I wrote specific tests in the app itself, and held off writing generic tests for the package until the structure had stabilised. Of course, once the API was stable and the app was deployed I never got around to writing those tests or documentation for the package as planned...

For my own needs this situation is fine, I'm confident in the test suite of my app. Should others show interest in this package I'd be willing to put some effort in to set up proper testing and documentation for release of a stable `1.0` version. Please [get in touch](https://github.com/bjuppa/eloquent-state-machine/issues/5) if this is something you'd like to see happen!

Requirements
------------

[](#requirements)

Row-level locking is only supported in MySQL / MariaDB and PostgreSQL.

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

[](#installation)

You can install the package via composer:

```
composer require bjuppa/eloquent-state-machine
```

Usage
-----

[](#usage)

Start by drawing a *statechart* for your state machine, without it you will probably miss some state, transition, event or action.

**Everything** related to a specific state in the chart is coded directly into distinct methods of that state's PHP class.

### Security

[](#security)

If you discover any security related issues, please email  instead of using the issue tracker.

Credits
-------

[](#credits)

- [Björn Nilsved](https://github.com/bjuppa)
- [All Contributors](../../contributors)

License
-------

[](#license)

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

###  Health Score

17

—

LowBetter than 6% of packages

Maintenance7

Infrequent updates — may be unmaintained

Popularity13

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity33

Early-stage or recently created project

 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

Unknown

Total

1

Last Release

982d ago

### Community

Maintainers

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

---

Top Contributors

[![bjuppa](https://avatars.githubusercontent.com/u/5339269?v=4)](https://github.com/bjuppa "bjuppa (128 commits)")

---

Tags

laraveleloquentstate-machine

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/bjuppa-eloquent-state-machine/health.svg)

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

###  Alternatives

[cviebrock/eloquent-sluggable

Easy creation of slugs for your Eloquent models in Laravel

4.0k13.6M251](/packages/cviebrock-eloquent-sluggable)[tucker-eric/eloquentfilter

An Eloquent way to filter Eloquent Models

1.8k4.8M26](/packages/tucker-eric-eloquentfilter)[watson/validating

Eloquent model validating trait.

9723.3M46](/packages/watson-validating)[cybercog/laravel-ban

Laravel Ban simplify blocking and banning Eloquent models.

1.1k651.8k11](/packages/cybercog-laravel-ban)[cybercog/laravel-love

Make Laravel Eloquent models reactable with any type of emotions in a minutes!

1.2k302.7k1](/packages/cybercog-laravel-love)[cviebrock/eloquent-taggable

Easy ability to tag your Eloquent models in Laravel.

567694.8k3](/packages/cviebrock-eloquent-taggable)

PHPackages © 2026

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