Getting started
The state machine is a flexible library that helps you move Eloquent models from States through Transitions while emitting events along the way.
Composer installation
Install the package using the usual method with composer in your terminal.
composer require hyn/state-machine
Laravel integration
The package ships with an easy to use Processor command to move models through the state machine in the background. In order to use this command, you need to register the provider in your config/app.php
.
'providers' => [
...
Hyn\Statemachine\Providers\StatemachineProvider::class,
];
Second thing you have to arrange is adding a column state
to each model that will implement the state machine and that it implements the ProcessedByStatemachine interface.
<?php
namespace App;
use Hyn\Statemachine\Contracts\ProcessedByStatemachine;
use Illuminate\Database\Eloquent\Model;
class User extends Model implements ProcessedByStatemachine
{
}
As a last step you have to publish the state-machine
configuration file and nurture it. The file should hold all state machine definition class names.
php artisan vendor:publish --provider="Hyn\Statemachine\Providers\StatemachineProvider"
Now make sure the configuration file has all your definitions registered.
<?php
return [
/**
* Add all state machine definitions here.
*/
'definitions' => [
// \Hyn\Statemachine\Stubs\Definitions\CatDefinition::class
]
];
DON'T FORGET
Your model has to have the state column!
The processor
The package has a useful cronnable script called the processor. Simply define a cronjob that runs the following command regularly will make sure that models of all configured definitions are automatically moved into a possible next state through a transition.
php artisan state-machine:processor
Updated less than a minute ago