use Ashraf\EloquentStateMachine\StateMachines\StateMachine;
class StatusStateMachine extends StateMachine
{
public function recordHistory(): bool
{
return false;
}
public function transitions(): array
{
return [
//
];
}
public function defaultState(): ?string
{
return null;
}
}
public function transitions(): array
{
return [
'pending' => [
'approved' => fn($model, $who): bool => true,
'declined' => fn($model, $who): bool => $who->getTable() === User::tableName(), // only allow users to decline
],
'approved' => [
'processed' // no need for extra validation
],
];
}
public function transitions(): array
{
return [
'*' => ['approved', 'declined'], // From any to 'approved' or 'declined'
'approved' => '*', // From 'approved' to any
'*' => '*', // From any to any
];
}
public function defaultState(): ?string
{
return 'pending'; // it can be null too
}
public function recordHistory(): bool
{
return true;
}
use Ashraf\EloquentStateMachine\Traits\HasStateMachines;
use App\StateMachines\StatusStateMachine;
class SalesOrder extends Model
{
Use HasStateMachines;
/**
* mark the `status` to be controlled by `StatusStateMachine`
*/
public $stateMachines = [
'status' => StatusStateMachine::class
];
}
class StatusStateMachine extends StateMachine
{
public function beforeTransitionHooks(): array
{
return [
'approved' => [
function ($to, $model) {
// Dispatch some job BEFORE "approved changes to $to"
},
function ($to, $model) {
// Send mail BEFORE "approved changes to $to"
},
],
];
}
public function afterTransitionHooks(): array
{
return [
'processed' => [
function ($from, $model) {
// Dispatch some job AFTER "$from transitioned to processed"
},
function ($from, $model) {
// Send mail AFTER "$from transitioned to processed"
},
],
];
}
}