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.
Download iotron/laravel-state-machine
More information about iotron/laravel-state-machine
Files in iotron/laravel-state-machine
Package laravel-state-machine
Short Description A robust, enum-aware state machine for Laravel Eloquent models
License MIT
Homepage https://iotron.co
Informations about the package laravel-state-machine
Laravel State Machine
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
- Native BackedEnum support — use enums everywhere, normalized internally
- N+1 query prevention — eager-load history, get zero-query lookups in loops
- Transaction-safe transitions — model save + history recording are atomic
- Lifecycle events —
TransitionStarted,TransitionCompleted,TransitionFailed - Validation hooks — block invalid transitions with Laravel Validator
- Before/after hooks — run closures on specific state changes
- Pending transitions — schedule future state changes with jobs
- History tracking — full audit trail with custom properties and changed attributes
- Safe auth resolution — no crashes in queue/CLI contexts
- Artisan generator —
php artisan make:state-machine
Requirements
- PHP 8.2+
- Laravel 11 or 12
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
illuminate/contracts Version ^11.0||^12.0
illuminate/database Version ^11.0||^12.0
illuminate/support Version ^11.0||^12.0