2017 © Pedro Peláez
 

library eloquent-state-machine

Eloquent State Machine

image

bidzm/eloquent-state-machine

Eloquent State Machine

  • Saturday, February 17, 2018
  • by abidnurulhakim
  • Repository
  • 0 Watchers
  • 0 Stars
  • 177 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 9 Versions
  • 9 % Grown

The README.md

laravel-state-machine

Eloquent State Machine, (*1)

Install through Composer, (*2)

composer require bidzm/eloquent-state-machine

Usage

Just add Bidzm\StateMachine to your eloquent model., (*3)

namespace App;

use Illuminate\Database\Eloquent\Model;
use Bidzm\StateMachine;

class Message extends Model
{
    use StateMachine;
    protected $fieldState = 'state'; // attribute name that use as state. Default `state`;
    protected $initialState = 'initiated'; // initial state. Default `initiated`;
    protected $transitions = [
        [
            'from' => ['initiated'], // Previous state
            'to' => 'queued', // State name
            'on' => 'queue' // Action name
        ],
        [
            'from' => ['queued'], // Previous state
            'to' => 'finished', // State name
            'on' => 'finish' // Action name
        ],
    ];
}

There some function that you can use, (*4)

<?php

$message = new App\Message();
$message->save();
$message->state; // `initiated`

$message->can('queue'); // `true`
// `canQueued` method on your model where `Queued` is the "studly" cased name of the state you want check
$message->canQueued(); // `true`
$message->canFinished(); // `false`

$message->isState('initiated'); // `true`
// `isInitiated` method on your model where `Initiated` is the "studly" cased name of the state you want check
$message->isInitiated(); // `true`
$message->isFinished(); // `false`

$message->queue();
$message->state; // `queued`

If you want doing some action in after or before transition fired. You must override method beforeTransition() or afterTransition(), (*5)

<?php

public function beforeTransition($from, $to)
{
    // do something
}

public function afterTransition($from, $to)
{
    // do something
}

State Change At

You can store when state changed, with attribute state_change_at in your table, (*6)

namespace App;

use Illuminate\Database\Eloquent\Model;
use Bidzm\StateMachine;

class Message extends Model
{
    use StateMachine;
    protected $fieldState = 'state'; // Attribute name that use as state. Default `state`;
    protected $initialState = 'initiated'; // Initial state. Default `initiated`;
    protected $useChangeAt = true; // Determine if store time when state changed. Default false;
    protected $transitions = [
        [
            'from' => ['initiated'], // Previous state
            'to' => 'queued', // State name
            'on' => 'queue' // Name action
        ],
    ];
}

There some function that you can use, (*7)

<?php

$message = new App\Message();
$message->save();
$message->state; // `initiated`
$message->initiatedAt(); // 2017-08-19T21:07:00+07:00

$message->queue();
$message->state; // `queued`
$message->initiatedAt(); // 2017-08-19T21:07:00+07:00
$message->queuedAt(); // 2017-08-19T21:07:01+07:00

License

MIT - http://opensource.org/licenses/MIT, (*8)

The Versions

17/02 2018

dev-master

9999999-dev https://github.com/abidnurulhakim/laravel-state-machine

Eloquent State Machine

  Sources   Download

MIT

The Requires

 

by Abid Nurul Hakim

laravel eloquent state machine

17/02 2018

1.0.5

1.0.5.0 https://github.com/abidnurulhakim/laravel-state-machine

Eloquent State Machine

  Sources   Download

MIT

The Requires

 

by Abid Nurul Hakim

laravel eloquent state machine

17/02 2018

1.0.4

1.0.4.0 https://github.com/abidnurulhakim/laravel-state-machine

Eloquent State Machine

  Sources   Download

MIT

The Requires

 

by Abid Nurul Hakim

laravel eloquent state machine

19/08 2017

1.0.3.x-dev

1.0.3.9999999-dev https://github.com/abidnurulhakim/laravel-state-machine

Eloquent State Machine

  Sources   Download

MIT

The Requires

 

by Abid Nurul Hakim

laravel eloquent state machine

19/08 2017

1.0.3

1.0.3.0 https://github.com/abidnurulhakim/laravel-state-machine

Eloquent State Machine

  Sources   Download

MIT

The Requires

 

by Abid Nurul Hakim

laravel eloquent state machine

19/08 2017

1.0.2.x-dev

1.0.2.9999999-dev https://github.com/abidnurulhakim/laravel-state-machine

Eloquent State Machine

  Sources   Download

MIT

The Requires

 

by Abid Nurul Hakim

laravel eloquent state machine

19/08 2017

1.0.2

1.0.2.0 https://github.com/abidnurulhakim/laravel-state-machine

Eloquent State Machine

  Sources   Download

MIT

The Requires

 

by Abid Nurul Hakim

laravel eloquent state machine

19/08 2017

1.0

1.0.0.0 https://github.com/abidnurulhakim/laravel-state-machine

Eloquent State Machine

  Sources   Download

MIT

The Requires

 

by Abid Nurul Hakim

laravel eloquent state machine

19/08 2017

1.0.1

1.0.1.0 https://github.com/abidnurulhakim/laravel-state-machine

Eloquent State Machine

  Sources   Download

MIT

The Requires

 

by Abid Nurul Hakim

laravel eloquent state machine