PHPackages                             simon-roland/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. simon-roland/state-machine

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

simon-roland/state-machine
==========================

A Laravel state machine trait for managing state transitions.

v1.0.0(1y ago)02MITPHPPHP ^8.1

Since Nov 19Pushed 1y ago1 watchersCompare

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

READMEChangelogDependencies (1)Versions (2)Used By (0)

State Machine
=============

[](#state-machine)

A Laravel trait for managing state transitions.

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

[](#installation)

Install the package via composer:

```
composer require simon-roland/state-machine
```

Usage
-----

[](#usage)

To use the HasStateMachine trait, include it in your Eloquent model and implement the required methods.

```
use Illuminate\Database\Eloquent\Model;
use SimonRoland\StateMachine\Traits\HasStateMachine;

class Order extends Model
{
    use HasStateMachine;

    protected $fillable = ['state'];

    /**
     * Define the attribute that holds the current state.
     *
     * @return string
     */
    protected function getStateAttributeName(): string
    {
        return 'state';
    }

    /**
     * Define the allowed state transitions.
     *
     * @return array
     */
    protected function getAllowedTransitions(): array
    {
        return [
            'pending' => ['approved', 'rejected'],
            'approved' => ['shipped'],
            'shipped' => ['delivered', 'returned'],
        ];
    }
}
```

Example Workflow
----------------

[](#example-workflow)

Here’s how you can interact with the state machine in your code:

```
use App\Models\Order;

// Create a new order
$order = Order::create(['state' => 'pending']);

// Check the current state
echo $order->state; // Outputs: "pending"

// Check if a state transition is possible
if ($order->canTransitionTo('approved')) {
    $order->transitionTo('approved');
    echo $order->state; // Outputs: "approved"
} else {
    echo "Cannot transition to 'approved'.";
}

// Attempting an invalid transition throws an exception
try {
    $order->transitionTo('delivered'); // Invalid transition
} catch (\SimonRoland\StateMachine\Exceptions\InvalidStateTransitionException $e) {
    echo $e->getMessage(); // Outputs: "Invalid transition from 'approved' to 'delivered'."
}

// Successful transition
$order->transitionTo('shipped');
echo $order->state; // Outputs: "shipped"
```

Working with Enums
------------------

[](#working-with-enums)

You can use enums to define the allowed states and transitions:

```
namespace App\Enums;

enum OrderState: int
{
    case PENDING = 1;
    case APPROVED = 2;
    case REJECTED = 3;
    case SHIPPED = 4;
    case DELIVERED = 5;
    case RETURNED = 6;
}
```

```
use App\Enums\OrderState;
use Illuminate\Database\Eloquent\Model;
use SimonRoland\StateMachine\Traits\HasStateMachine;

class Order extends Model
{
    use HasStateMachine;

    protected $fillable = ['state'];

    protected $casts = [
        'state' => OrderState::class,
    ];

    /**
     * Define the attribute that holds the current state.
     *
     * @return string
     */
    protected function getStateAttributeName(): string
    {
        return 'state';
    }

    /**
     * Define the allowed state transitions.
     *
     * @return array
     */
    protected function getAllowedTransitions(): array
    {
        return [
            OrderState::PENDING->value => [OrderState::APPROVED, OrderState::REJECTED],
            OrderState::APPROVED->value => [OrderState::SHIPPED],
            OrderState::SHIPPED->value => [OrderState::DELIVERED, OrderState::RETURNED],
        ];
    }
}
```

Contributing
------------

[](#contributing)

Contributions are welcome! Feel free to open issues or submit pull requests.

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance38

Infrequent updates — may be unmaintained

Popularity2

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity47

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

Unknown

Total

1

Last Release

545d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/ba51711e1058b6f098745ea8fa52507250647e41c39d00be5544cde4e89c946d?d=identicon)[simon-roland](/maintainers/simon-roland)

---

Top Contributors

[![simon-roland](https://avatars.githubusercontent.com/u/40601765?v=4)](https://github.com/simon-roland "simon-roland (3 commits)")

### Embed Badge

![Health badge](/badges/simon-roland-state-machine/health.svg)

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

###  Alternatives

[wireui/wireui

TallStack components

1.8k1.3M16](/packages/wireui-wireui)[livewire/volt

An elegantly crafted functional API for Laravel Livewire.

4195.3M84](/packages/livewire-volt)[ramonrietdijk/livewire-tables

Dynamic tables for models with Laravel Livewire

21147.4k](/packages/ramonrietdijk-livewire-tables)

PHPackages © 2026

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