PHPackages                             louishrg/state-flow - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. louishrg/state-flow

ActiveLibrary

louishrg/state-flow
===================

Simple implementation of state machine for Laravel

1.5(6mo ago)141.8k1MITPHPPHP ^7.2|^8.1|^8.2|^8.3CI passing

Since Oct 1Pushed 6mo ago1 watchersCompare

[ Source](https://github.com/LouisHrg/state-flow)[ Packagist](https://packagist.org/packages/louishrg/state-flow)[ Docs](https://github.com/louishrg/state-flow)[ RSS](/packages/louishrg-state-flow/feed)WikiDiscussions master Synced 5d ago

READMEChangelog (10)Dependencies (4)Versions (17)Used By (0)

Laravel State Flow
==================

[](#laravel-state-flow)

[![Latest Version on Packagist](https://camo.githubusercontent.com/38df0926b47162a17b5e998263390dc26147a585f0f8d19042829ee8cc66c362/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6c6f7569736872672f73746174652d666c6f772e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/louishrg/state-flow)[![GitHub Tests Action Status](https://camo.githubusercontent.com/70c1664419cf0e390bee81a637e3a406bf690dcc02a32722b82dfd633a631bf8/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f6c6f7569736872672f73746174652d666c6f772f72756e2d74657374733f6c6162656c3d7465737473)](https://github.com/louishrg/state-flow/actions?query=workflow%3Arun-tests+branch%3Amaster)[![Total Downloads](https://camo.githubusercontent.com/0c91d403e73308de492118a358f0f0816a141b7e1c44b53b56791daa7abf117a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6c6f7569736872672f73746174652d666c6f772e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/louishrg/state-flow)

Simple state machine / resilient states for your Laravel application!

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require louishrg/state-flow
```

Create your states classes with artisan :
-----------------------------------------

[](#create-your-states-classes-with-artisan-)

Creating files for every available states is repetitive, that's why this package provide an artisan command to speed up the process :

```
php artisan states:new
```

Simple states AKA Stack
-----------------------

[](#simple-states-aka-stack)

A Stack is a simple state machine that doesn't need to register transitions. It's a very convenient way to add a hardcoded type or category to your model.

#### How to use :

[](#how-to-use-)

You need at least 1 variable: **key** which is the real value of the column in the database.

If you want to use other variables as key, you can give the name of the variable in you stateStack creation (see below)

- First parameter is all your available state as an array.
- Second parameter is the default value when creating a model (optional).
- Third parameter is to override the default key-value (optional).

```
new Stack(self::$status, Pending::class, 'key'),
```

Declare your states classes in the directory of your choice, for example :

```
namespace App\Models\States\User;

use Louishrg\StateFlow\StateAbstract;

class Active extends StateAbstract
{
    public $key = 'active';

    public $label = 'Active';
    public $color = 'green';
    // and everything you want !
    //
    public function computedField() {
        return $this->$label.$this->color;
    }
}
```

Now, add all the needed declaration in your model :

```
