1. Go to this page and download the library: Download changole/laravel-workflows 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/ */
namespace App\Workflows;
use App\Models\Post;
use Changole\Workflows\Core\WorkflowDefinition;
use Changole\Workflows\Core\WorkflowContext;
class PostWorkflow extends WorkflowDefinition
{
public function model(): string
{
return Post::class;
}
public function initialState(): string
{
return 'draft';
}
public function transitions(): array
{
return [
$this->transition('submit')->from('draft')->to('pending'),
$this->transition('approve')
->from('pending')
->to('approved')
->guard(fn (WorkflowContext $ctx) => (bool) ($ctx->meta['can_approve'] ?? false), 'Not allowed'),
$this->transition('reject')->from('pending')->to('rejected'),
];
}
}
namespace App\Models;
use Changole\Workflows\Traits\HasWorkflow;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
use HasWorkflow;
protected string $workflow = \App\Workflows\PostWorkflow::class;
}