A finite state machine works with laravel eloquent model
chareice/laravel-eloquent-fsm is a Laravel package for a finite state machine works with laravel eloquent model.
It currently has 5 GitHub stars and 0 downloads on Packagist.
Install it with composer require chareice/laravel-eloquent-fsm.
Discover more Laravel packages by chareice
or browse all Laravel packages to compare alternatives.
Last updated
English | 中文
You can install the package via composer:
composer require chareice/laravel-eloquent-fsm
You can publish and run the migrations with:
php artisan vendor:publish --tag="eloquent-fsm-migrations"
php artisan migrate
use \Chareice\LaravelEloquentFSM\HasStateMachine;
use \Chareice\LaravelEloquentFSM\StateMachineModelInterface;
class Order extends Model implements StateMachineModelInterface {
use HasStateMachine;
public int $counter = 0;
// define event
public function events(): array
{
return [
Event::builder()
->setName(OrderStateEvent::PAY)
->setFrom(OrderState::PENDING)
->setTo(OrderState::PAID)
->setAfter(function () {
$this->counter = 1;
})
->build(),
Event::builder()
->setName(OrderStateEvent::SHIP)
->setFrom(OrderState::PAID)
->setTo(OrderState::SHIPPED)
->build(),
];
}
}
$order = Order::first();
// run event
$order->getStateMachine()->runEvent(OrderStateEvent::PAY);
// check new state
$order->getStateMachine()->getCurrentState(); // equals OrderState::PAID
// check event log
$order->stateMachineLogs()->first();
composer test
The MIT License (MIT). Please see License File for more information.