PHP code example of istvan0304 / laravel-workflow

1. Go to this page and download the library: Download istvan0304/laravel-workflow 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/ */

    

istvan0304 / laravel-workflow example snippets

`
namespace App\Models;

use Illuminate\Support\Facades\Auth;
use Istvan0304\Workflow\WorkflowDefinition;

class BlogPostWorkflow implements WorkflowDefinition
{
    const DRAFT = 'draft';
    const DELETED = 'deleted';
    const FINAL = 'final';
    const REJECT = 'reject';
    const FIX = 'fix';
    const PUBLISHED = 'published';

    /**
     * @return string[]
     */
    public static function statusLabels(): array
    {
        return [
            self::DRAFT => 'Draft',
            self::DELETED => 'Deleted',
            self::FINAL => 'Finalized',
            self::REJECT => 'Rejected',
            self::FIX => 'Returned for repair',
            self::PUBLISHED => 'Published'
        ];
    }

    /**
     * @return string[]
     */
    public static function statusActionLabels(): array
    {
        return [
            self::DRAFT => 'Draft',
            self::DELETED => 'Delete',
            self::FINAL => 'Finalization',
            self::REJECT => 'Rejection',
            self::FIX => 'Return for repair',
            self::PUBLISHED => 'Publication'
        ];
    }

    /**
     * @return array
     */
    public static function getDefinition(): array
    {
        return [
            'initialStatus' => self::DRAFT,
            'status' => [
                self::DRAFT => [
                    'transition' => [self::FINAL, self::DELETED],
//                    'transition' => function(){
//                        return [self::REJECT, self::FIX];
//                    },
                ],
                self::DELETED => [
                    'transition' => [],
                ],
                self::FINAL => [
                    'transition' => (Auth::user()->hasRole('admin') ? [self::DELETED, self::REJECT, self::FIX, self::PUBLISHED] : [self::REJECT, self::FIX, self::PUBLISHED]),
                ],
                self::REJECT => [
                    'transition' => [],
                ],
                self::FIX => [
                    'transition' => [self::FINAL],
                ],
                self::PUBLISHED => [
                    'transition' => [],
                ]
            ]
        ];
    }
}
`
/**
     * Create blog post
     * @param Request $request
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|void
     */
    public function createStore(Request $request)
    {
        $model = new BlogPost();
        
        // ...

        $model->start();     // Workflow start
        $model->fill($attributes);

        if ($model->save()) {
            return redirect('/blog-posts')->with('message', 'Successfully saved!');
        }
    }
`
/**
     * @param Request $request
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|void
     */
    public function updateStore(Request $request, $id)
    {
        $model = BlogPost::find($id);
        
        // ...

        if($request->final){
            $model->sendToStatus(BlogPostWorkflow::FINAL);  // Change status
        }

        // ...
    }