chareice/laravel-eloquent-fsm

A finite state machine works with laravel eloquent model

dev-main 2023-07-03 19:05 UTC

README

English | 中文

A finite state machine works with laravel eloquent model

Installation

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

Usage

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();

Testing

composer test

Credits

License

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