New

Just launched: Vueblocks, a brand-new search engine for Vue packages! Explore Vueblocks →

canyongbs/model-state-machine

Model State Machine
29,177 9

Larablocks Analysis

canyongbs/model-state-machine works with Laravel 12 and 13 (latest release 2.1.1, Nov 16, 2026).

Laravel 9, 10 and 11 in earlier releases.

Recommendation checks

Solid choice

Solid choice: maintained and up to date, meets 2 of 4 checks; below the bar for Recommended on use.

  • Works with Laravel 13, the current version
  • Released in the last 12 months (Nov 16, 2026)
  • 2,876 downloads a month (needs 10,000)
  • 9 GitHub stars (needs 100)

State

Actively maintained
  • Last stable release: Nov 16, 2026.
  • 8 releases in the last 12 months.
  • First released: Dec 6, 2024.
  • Monthly downloads: 2,876.
  • 0 open issues on GitHub.

Data from Packagist and GitHub, checked by Larablocks on Nov 27, 2026.

Install
composer require canyongbs/model-state-machine
Latest Version:2.1.1
PHP:^8.2
Laravel:12 · 13
Older releases:9 · 10 · 11
License:MIT
Last Updated:Nov 16, 2026
Links: GitHub  ·  Packagist
Maintainer: canyongbs

Laravel Model State Machine

This package adds support for creating state machines for attributes on Laravel Eloquent Models.

Requirements

  • PHP 8.2 or higher
  • Laravel 12.x or higher

Installation

You can install the package via composer:

composer require canyongbs/model-state-machine

Usage

For example, we have a blog system, and our blog posts have three statuses: draft, pending, and published. When we are writing a post, it's in the draft status. Whenever we finish writing our blog posts, we schedule them for publication in the future, which changes the status of the post to pending. Once the post is published, the status changes to published.

The simplest backed enum that holds the states of a blog post status is:

use Bvtterfly\ModelStateMachine\Attributes\AllowTransitionTo;
use Bvtterfly\ModelStateMachine\Attributes\InitialState;

enum PostState: string
{
    #[InitialState]
    #[AllowTransitionTo(self::PENDING)]
    case DRAFT = 'draft';

    #[AllowTransitionTo(self::PUBLISHED)]
    case PENDING = 'pending';

    case PUBLISHED = 'published';

}

Here's what the Blog post model would look like:

class Post extends Model
{
    use HasStateMachine;

    protected $casts = [
      'status' => PostState::class
    ];


    public function getStateMachineFields(): array
    {
        return [
            'status'
        ];
    }

}

A model can have as many state machine fields as you want, You need to add them to the list using the getStateMachineFields method.

Since State machine loads state configuration from string backed enums, You need to cast state machine fields to correlated state enums in your model.

Now, You can get your state machine:

$stateMachine = $post->getStateMachine('status')

Get All states

You can use the getAllStates method, which return collection of the all available states:

$stateMachine->getAllStates();

Get All allowed transitions

You can use the getStateTransitions method, which return collection of available transitions for current/initial state

$stateMachine->getStateTransitions();

If the state field is null and the state configuration doesn't have a initial state (field in unknown state), It will throw an exception.

If you want to get available transitions for a state, You can pass it to the method:

$stateMachine->getStateTransitions(PostState::PENDING);
// or $stateMachine->getStateTransitions('pending');

Using transitions

To use transitions, call the transitionTo method on the state field as follows:

$stateMachine->transitionTo(PostState::PUBLISHED);
// or $stateMachine->transitionTo('published');

You can pass array as a second argument to the transitionTo method for additional data that you'll need in your actions and transitions.

State Actions

You can add actions to run if a state changes to a state. In the above example, Maybe we want to send a tweet and send email to subscribers when the post is published.

We can do this using the #[Actions] attribute. Here's how our PostState would look like:

enum PostState: string
{
    #[InitialState]
    #[AllowTransitionTo(self::PENDING)]
    case DRAFT = 'draft';

    #[AllowTransitionTo(self::PUBLISHED)]
    case PENDING = 'pending';

    #[Actions(SendTweetAction::class, SendEmailToSubscribers::class)]
    case PUBLISHED = 'published';

}

Actions are classes that implements Bvtterfly\ModelStateMachine\Contracts\StateMachineAction:

class SendTweetAction implements StateMachineAction
{

    public function handle(Model $model, array $additionalData): void
    {
        // send tweet...
    }
}

Your actions may also type-hint any dependencies they need on their constructors. All actions are resolved via the Laravel service container, so dependencies will be injected automatically.

Transition Actions

In addition to state actions, maybe you want to run actions only when a specific state transit to another state.

You can pass array of actions as second argument to #[AllowTransitionTo].

In the above example, If we want to send a notification to the admin when the post status change to the pending, Our PostState would look like this:

enum PostState: string
{
    #[InitialState]
    #[AllowTransitionTo(self::PENDING, [SendNotificationToAdmin::class])]
    case DRAFT = 'draft';

    #[AllowTransitionTo(self::PUBLISHED)]
    case PENDING = 'pending';

    #[Actions(SendTweetAction::class, SendEmailToSubscribers::class)]
    case PUBLISHED = 'published';
}

Transition Actions run before State Actions

Action With Validation

When using transitions, you can pass additional data as a second argument, and this data will pass to all actions. So, It's necessary to validate this data before running actions.

Validators are actions that implement Bvtterfly\ModelStateMachine\Contracts\StateMachineValidation.

In above example, We want to send notification to admin when post status changes to pending:

$stateMachine->transitionTo(PostState::PENDING, [
    'message' => '...'
]);

Here's how our SendNotificationToAdmin action would look like:

class SendNotificationToAdmin implements StateMachineAction, StateMachineValidation
{

        public function validate(Model $model, array $additionalData): void
    {
        $validator = validator($additionalData, [
            'message' => 'required',
        ]);

        if ($validator->fails()) {
            // throw exception
        }
    }

    public function handle(Model $model, array $additionalData): void
    {
        // send notification...
    }
}

Custom transition Classes

This package comes with a default transition class that save new state after running State & Transition Actions. If you need to do more than just changing to the new state, you can use transition classes.

Custom transition are classes that implements Bvtterfly\ModelStateMachine\Contracts\StateTransition.

For example, We want to store the user_id of who changes the status of a post to pending status in the post model:

class DraftToPending implements StateTransition
{
    public function commitTransition(
            BackedEnum|string $newState,
            Model $model,
            string $field,
            array $additionalData
        ): void {
            $model->{$field} = $newState;
            $model->causer = $additionalData['user_id']
            $model->save();
        }
}

You can pass this class as a third argument to the #[AllowTransitionTo].

Then, Our PostState would look like this:

enum PostState: string
{
    #[InitialState]
    #[AllowTransitionTo(self::PENDING, [SendNotificationToAdmin::class], DraftToPending::class)]
    case DRAFT = 'draft';

    #[AllowTransitionTo(self::PUBLISHED)]
    case PENDING = 'pending';

    #[Actions(SendTweetAction::class, SendEmailToSubscribers::class)]
    case PUBLISHED = 'published';
}

Testing

composer test

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.

Related Packages

doctrine/dbal

Powerful PHP database abstraction layer (DBAL) with many features for database s...

670,032,257 9,703
laravel/framework

The Laravel Framework.

640,572,096 35,338
laravel/tinker

Powerful REPL for the Laravel framework.

546,909,908 7,440
laravel/serializable-closure

Laravel Serializable Closure provides an easy and secure way to serialize closur...

465,816,364 607
nunomaduro/collision

Cli error handling for console/command-line PHP applications.

431,187,642 4,660

Version History

Version Released PHP Laravel License
2.1.1 ^8.2 ^12.0|^13.0 MIT
2.1.0 ^8.2 ^12.0|^13.0 MIT
2.0.8 ^8.2 ^11.0|^12.0 MIT
2.0.7 ^8.2 ^11.0|^12.0 MIT
2.0.6 ^8.2 ^11.0|^12.0 MIT