PHPackages                             joggapp/eloquent-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. joggapp/eloquent-state-machines

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

joggapp/eloquent-state-machines
===============================

This Laravel package simplifies the creation of state machines for your Eloquent models

v3.0.0(2mo ago)040↑100%MITPHPPHP ^8.4

Since Jun 20Pushed 2mo ago3 watchersCompare

[ Source](https://github.com/JoggApp/eloquent-state-machines)[ Packagist](https://packagist.org/packages/joggapp/eloquent-state-machines)[ Docs](https://github.com/joggapp/eloquent-state-machines)[ RSS](/packages/joggapp-eloquent-state-machines/feed)WikiDiscussions master Synced 1w ago

READMEChangelog (8)Dependencies (3)Versions (10)Used By (0)

Introduction
------------

[](#introduction)

This package simplifies state transitions for Eloquent models by centralizing the transition logic in a single `StateMachine` class. It allows you to define available transitions between states and handle actions during each transition. Additionally, it automatically records the history of all state transitions for a model.

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

[](#installation)

Install the package via Composer:

```
composer require joggapp/eloquent-state-machines
```

Publish the package's configuration:

```
php artisan vendor:publish --provider="JoggApp\EloquentStateMachines\EloquentStateMachinesServiceProvider"
```

Run the migrations:

```
php artisan migrate
```

Usage
-----

[](#usage)

### Defining a StateMachine

[](#defining-a-statemachine)

Consider an `Invoice` model with a `status` field that can have one of the following values: 'draft', 'open', 'paid', 'void', or 'un-collectable'.

We can manage these states and their transitions within a single StateMachine class. To create one, use the following command:

```
php artisan make:state-machine InvoiceStatusStateMachine
```

This command will create a new StateMachine class in the App\\StateMachines directory.

Within this class, you can define the default state:

```
public static function defaultState(): ?string
{
    return 'draft';
}
```

Specify the allowed transitions:

```
public static function transitions(): array
{
    return [
        'draft' => ['open'],
        'open' => ['paid', 'void', 'un-collectable'],
        'un-collectable' => ['paid'],
        'paid' => null,
        'void' => null,
    ];
}
```

Enable or disable history logging:

```
public static function saveTransitions(): bool
{
    return true;
}
```

### Registering the StateMachine

[](#registering-the-statemachine)

After defining the StateMachine, register it in the Invoice model using the `$stateMachines` property.

```
use App\StateMachines\InvoiceStatusStateMachine;
use JoggApp\EloquentStateMachines\Traits\HasStateMachines;

class Invoice extends Model
{
    use HasStateMachines;

    protected $stateMachines = [
        'status' => InvoiceStatusStateMachine::class,
    ];
}
```

### State Machine Methods

[](#state-machine-methods)

By registering `$stateMachines` in your model, the `HasStateMachines` trait provides a method to interact with each state field. For example, `$invoice->status()` can be used to check the current state, apply transitions, and view state history.

### Checking State

[](#checking-state)

```
$invoice->status; // 'draft'

$invoice->status()->is('draft'); // true

$invoice->status()->isNot('draft'); // false
```

### Verify transition

[](#verify-transition)

```
$invoice->status; // 'draft'

$invoice->status()->canTransitionTo('open'); // true

$invoice->status()->canTransitionTo('void'); // false
```

### Transitioning States

[](#transitioning-states)

To transition from one state to another, use the transitionTo method:

```
$invoice->status()->transitionTo('open');
```

You can also pass additional data if needed:

```
$invoice->status()->transitionTo('open', [
    'terms' => 'net30',
]);
```

The state machine will verify if the transition is allowed based on the defined `transitions()`. If the transition is not allowed, an exception will be thrown.

### Querying History

[](#querying-history)

If `saveTransitions()` is set to `true` in the StateMachine, each state transition will be recorded in the `state_transitions` table created during installation.

```
$invoice->status()->history();
```

Advanced Usage
--------------

[](#advanced-usage)

### Override canTransitionTo() Method

[](#override-cantransitionto-method)

By default the `canTransitionTo()` method uses the transitions defined in our StateMachine's `transitions()` method. It is possible to override any of the transitions by adding a method with the naming convention `canTransitionFrom{Studly Case From State}To{Studly Case To State}()`.

```
public function canTransitionFromDraftToOpen(array $data): bool
{
    return $this->model->tasks()->notCompleted()->isEmpty();
}
```

### Override transitionTo() Method

[](#override-transitionto-method)

Similar to the `canTransitionTo()` method, the `transitionTo()` method can also be modified for any transition. The naming convention for this is:

- `beforeTransitionFrom{Studly Case From State}To{Studly Case To State}()`
- `beforeTransitionFrom{Studly Case From State}()`
- `beforeTransitionTo{Studly Case To State}()`
- `afterTransitionFrom{Studly Case From State}To{Studly Case To State}()`
- `afterTransitionFrom{Studly Case From State}`
- `afterTransitionTo{Studly Case To State}()`

```
public function beforeTransitionFromDraftToOpen(array $data): void
{
    $this->model->sendPaymentNotification();
}

public function beforeTransitionFromOpen(array $data): void
{
    // Do something before transitioning away from "open" to any state
}

public function afterTransitionFromOpenToVoid(array $data): void
{
    $this->model->delete();
}

public function afterTranstionToPaid(array $data): void
{
    // Do something after transitioning to "paid" from any state
}
```

###  Health Score

45

—

FairBetter than 91% of packages

Maintenance84

Actively maintained with recent releases

Popularity11

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity64

Established project with proven stability

 Bus Factor1

Top contributor holds 89.5% 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 ~91 days

Recently: every ~136 days

Total

8

Last Release

81d ago

Major Versions

v1.2.0 → v2.0.02025-03-26

v2.0.0 → v3.0.02026-03-20

PHP version history (2 changes)v1.0.0PHP ^8.3

v2.0.0PHP ^8.4

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/11228182?v=4)[Harish Toshniwal](/maintainers/introwit)[@introwit](https://github.com/introwit)

---

Top Contributors

[![danielluong](https://avatars.githubusercontent.com/u/6686376?v=4)](https://github.com/danielluong "danielluong (17 commits)")[![introwit](https://avatars.githubusercontent.com/u/11228182?v=4)](https://github.com/introwit "introwit (2 commits)")

---

Tags

joggappeloquent-state-machines

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/joggapp-eloquent-state-machines/health.svg)

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

###  Alternatives

[markwalet/nova-modal-response

A Laravel Nova asset for Modal responses on an action.

17818.7k](/packages/markwalet-nova-modal-response)[crumbls/layup

A visual page builder plugin for Filament 5 — Divi-style grid layouts with extensible widgets.

591.7k1](/packages/crumbls-layup)[tomshaw/electricgrid

A feature-rich Livewire package designed for projects that require dynamic, interactive data tables.

119.2k](/packages/tomshaw-electricgrid)

PHPackages © 2026

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