PHP code example of szana8 / laraflow

1. Go to this page and download the library: Download szana8/laraflow library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

szana8 / laraflow example snippets


composer 

php artisan migrate

 $table->string('status');

use szana8\Laraflow\Traits\Flowable;

class SampleClass extends Model {

    use Flowable;


[
    'default' => [
        //... see published configuration example in config directory.
    ]
];

[
    'statemachine_name1' => [ // might also be called 'default' !
        // ...  see published configuration example in config directory
    ],
    'statemachine_name2' => [
        // ...  see published configuration example in config directory
    ],
]

// when using only 1 or at least 'default' named state machine.
$object->transiton($new_status);
// when using only 1 not named state machine.
$statemachineName1 = 'statemachine_name1'
$object->transiton($new_status, $statemachineName1);
$statemachineName2 = 'default'
$object->transiton($new_status, $statemachineName2);
$object->save();

/**
* Return historical records.
*
* @return string
*/
public function getFlowHistoryAttribute()
{
return $this->history();
}

LaraflowEvents::PRE_TRANSITION
LaraflowEvents::POST_TRANSITION
LaraflowEvents::CAN_TRANSITION

class TestValidator extends Rule implements LaraflowValidatorInterface
{
    /**
     * Validate the attributes with the given rules.
     *
     * @param array $attributes
     * @param array $rules
     * @return mixed
     */
    public function validate(array $attributes, array $rules)
    {
        if (1 == 2) {
            return true;
        }

        throw LaraflowValidatorException::withMessages(['Validation error']);
    }
}

class TestPreCallback implements LaraflowCallbackInterface
{
    public function handle(LaraflowTransitionEvents $event)
    {
        CallbackTest::insert(['message' => json_encode($event->convertToArray())]);
    }
}

class LaraflowEventSubscriber
{
    /**
     * Register the listeners for the subscriber.
     *
     * @param  \Illuminate\Events\Dispatcher  $events
     */
    public function subscribe($events)
    {
        $events->listen(
            LaraflowEvents::PRE_TRANSITION,
            'App\TestPreCallback@handle'
        );
    }
}

php artisan vendor:publish --provider="szana8\Laraflow\LaraflowServiceProvider"
properties
foo@bar: php artian laraflow:make subscriber --NameOfTheSubscriber
properties
foo@bar: php artian laraflow:make validator --NameOfTheValidator