Download the PHP package iotron/laravel-state-machine without Composer

On this page you can find all versions of the php package iotron/laravel-state-machine. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package laravel-state-machine

Laravel State Machine

Latest Version on Packagist GitHub Tests Action Status Total Downloads

A robust, enum-aware state machine for Laravel Eloquent models. Define allowed transitions, validate before state changes, track full history, and prevent N+1 queries — all with native PHP BackedEnum support and zero dependencies beyond Laravel.

Features

Requirements

Installation

Publish the config file:

Publish and run the migrations:

Migrating from asantibanez/laravel-eloquent-state-machines? See the Migration Guide below — no database changes needed.

Quick Start

1. Create a State Machine

Define your transitions and default state:

2. Add the Trait to Your Model

3. Use It

Configuration

Defining State Machines

Transitions

The transitions() method returns a map of from => [allowed targets]:

Default State

Set the initial state for new models:

Record History

Control whether transitions are tracked (default: true):

Validation

Return a Validator to block transitions that don't meet requirements:

If the validator fails, a ValidationException is thrown and the model stays unchanged.

Before/After Hooks

Run closures when entering or leaving specific states. All hooks receive ($from, $to, $model):

State Proxy API

Calling $model->status() returns a State proxy with these methods:

Method Returns Description
state() string Current state (normalized to string)
is($state) bool Check if current state matches
isNot($state) bool Check if current state doesn't match
canBe($state) bool Check if transition is allowed
transitionTo($state, $props, $responsible) void Execute transition
postponeTransitionTo($state, $when, ...) ?PendingTransition Schedule future transition
was($state) bool Ever been in this state?
timesWas($state) int Count times in this state
whenWas($state) ?Carbon When last entered this state
snapshotWhen($state) ?Transition Transition record for a state
snapshotsWhen($state) Collection All records for a state
history() Builder Query builder for this field's history
latest() ?Transition Most recent transition to current state
getCustomProperty($key) mixed Custom property from latest transition
responsible() ?Model User who triggered latest transition
allCustomProperties() array All custom properties from latest
pendingTransitions() Builder Query pending transitions
hasPendingTransitions() bool Any pending transitions?

All methods accept both strings and BackedEnum values.

N+1 Prevention

When you eager-load stateHistory, all history lookups use the in-memory collection — zero extra queries:

Without eager loading, each call falls back to a database query automatically.

Events

Three events fire during transitions for app-wide listening:

Event When Payload
TransitionStarted Before hooks fire $model, $field, $from, $to
TransitionCompleted After everything succeeds $model, $field, $from, $to
TransitionFailed On any exception $model, $field, $from, $to, $exception

Pending Transitions

Schedule transitions to execute in the future:

Add the dispatcher job to your scheduler:

The job processes pending transitions in chunks and dispatches each as a separate queued job for reliability.

Transition Model

The Transition model (stored in the state_histories table by default) includes useful scopes:

Migrating from asantibanez/laravel-eloquent-state-machines

This package is a drop-in replacement. No database migration needed — it reads the same state_histories table by default.

Step 1: Install

Step 2: Update model imports

Step 3: Update state machine base class

Step 4: Update hook signatures

The old package used ($from, $model) / ($to, $model). This package uses a consistent ($from, $to, $model) for both before and after hooks:

Step 5: Remove old packages

Step 6: Enable native enum casts

You can now use Laravel's native enum cast — no more workarounds:

What's different?

Feature Old Package This Package
Enum support Manual workarounds Native BackedEnum
N+1 prevention Not built-in Built-in via eager loading
Transaction safety No wrapping DB::transaction()
Hook arguments Inconsistent ($to, $model) / ($from, $model) Consistent ($from, $to, $model)
Events None 3 lifecycle events
Auth in queues Crashes Safe fallback
Dependencies Requires laravel-macroable-models Zero external deps
Method resolution Static macros via reflection Native __call()

Testing

Credits

This package is inspired by and built upon the work of asantibanez/laravel-eloquent-state-machines by Andrés Santibáñez. The original package provided the foundation for Eloquent state machine management that this package extends with native BackedEnum support, N+1 prevention, transaction safety, lifecycle events, and other improvements.

Changelog

Please see CHANGELOG for more information on what has changed recently.

License

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


All versions of laravel-state-machine with dependencies

PHP Build Version
Package Version
Requires php Version ^8.2
illuminate/contracts Version ^11.0||^12.0
illuminate/database Version ^11.0||^12.0
illuminate/support Version ^11.0||^12.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package iotron/laravel-state-machine contains the following files

Loading the files please wait ...