PHP code example of jasperfernandez / laraflow

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

    

jasperfernandez / laraflow example snippets




use App\Models\Action;
use App\Models\Role;
use App\Models\Status;
use JasperFernandez\Laraflow\Models\WorkflowInstance;
use JasperFernandez\Laraflow\Models\WorkflowInstanceStep;
use JasperFernandez\Laraflow\Models\WorkflowInstanceStepAssignment;
use JasperFernandez\Laraflow\Models\WorkflowInstanceTransition;

return [
    'table_names' => [
        'workflow_templates' => 'workflow_templates',
        'workflow_template_steps' => 'workflow_template_steps',
        'workflow_template_step_assignments' => 'workflow_template_step_assignments',
        'workflow_template_step_actions' => 'workflow_template_step_actions',
        'workflow_instances' => 'workflow_instances',
        'workflow_instance_steps' => 'workflow_instance_steps',
        'workflow_instance_step_assignments' => 'workflow_instance_step_assignments',
        'workflow_instance_transitions' => 'workflow_instance_transitions',
    ],

    'column_names' => [
        'model_morph_key' => 'subject_id',
        'model_morph_type' => 'subject_type',
    ],

    'models' => [
        'role' => Role::class,
        'status' => Status::class,
        'action' => Action::class,
        'workflow_instance' => WorkflowInstance::class,
        'workflow_instance_step' => WorkflowInstanceStep::class,
        'workflow_instance_step_assignment' => WorkflowInstanceStepAssignment::class,
        'workflow_instance_transition' => WorkflowInstanceTransition::class,
    ],
];

use App\Models\Action;
use App\Models\Role;
use App\Models\Status;
use JasperFernandez\Laraflow\Models\WorkflowTemplate;
use JasperFernandez\Laraflow\Models\WorkflowTemplateStep;
use JasperFernandez\Laraflow\Models\WorkflowTemplateStepAction;
use JasperFernandez\Laraflow\Models\WorkflowTemplateStepAssignment;

$memberRole = Role::firstOrCreate(['name' => 'member']);
$reviewerRole = Role::firstOrCreate(['name' => 'reviewer']);

$completedStep = Status::firstOrCreate(
    ['code' => 'completed'],
    ['name' => 'Completed'],
);

$pendingReview = Status::firstOrCreate(
    ['code' => 'pending_eligibility_verification'],
    ['name' => 'Pending Eligibility Verification'],
);

$approvedStep = Status::firstOrCreate(
    ['code' => 'approved_step'],
    ['name' => 'Approved Step'],
);

$approvedApplication = Status::firstOrCreate(
    ['code' => 'approved'],
    ['name' => 'Approved'],
);

$submitAction = Action::firstOrCreate(
    ['code' => 'submit_application'],
    ['name' => 'Submit Application'],
);

$approveAction = Action::firstOrCreate(
    ['code' => 'approve_application'],
    ['name' => 'Approve Application'],
);

$template = WorkflowTemplate::create([
    'template_code' => 'MEMBERSHIP-APPLICATION',
    'template_name' => 'Membership Application Workflow',
    'description' => 'Workflow for membership application approvals',
    'is_active' => true,
]);

$registrationStep = WorkflowTemplateStep::create([
    'workflow_template_id' => $template->id,
    'step_code' => 'APPLICANT_REGISTRATION',
    'step_name' => 'Applicant Registration',
    'step_description' => 'Initial registration step',
    'sequence_no' => 1,
    'is_active' => true,
]);

$reviewStep = WorkflowTemplateStep::create([
    'workflow_template_id' => $template->id,
    'step_code' => 'ELIGIBILITY_REVIEW',
    'step_name' => 'Eligibility Review',
    'step_description' => 'Review submitted application',
    'sequence_no' => 2,
    'is_active' => true,
]);

WorkflowTemplateStepAssignment::create([
    'workflow_template_step_id' => $registrationStep->id,
    'role_id' => $memberRole->id,
]);

WorkflowTemplateStepAssignment::create([
    'workflow_template_step_id' => $reviewStep->id,
    'role_id' => $reviewerRole->id,
]);

WorkflowTemplateStepAction::create([
    'workflow_template_step_id' => $registrationStep->id,
    'action_id' => $submitAction->id,
    'next_workflow_template_step_id' => $reviewStep->id,
    'completes_step' => true,
    'resulting_step_status_id' => $completedStep->id,
    'resulting_subject_status_id' => $pendingReview->id,
    'closes_workflow' => false,
]);

WorkflowTemplateStepAction::create([
    'workflow_template_step_id' => $reviewStep->id,
    'action_id' => $approveAction->id,
    'next_workflow_template_step_id' => null,
    'completes_step' => true,
    'resulting_step_status_id' => $approvedStep->id,
    'resulting_subject_status_id' => $approvedApplication->id,
    'closes_workflow' => true,
]);

use App\Models\Application;
use JasperFernandez\Laraflow\Services\WorkflowEngine;

$application = Application::findOrFail(1);

$instance = app(WorkflowEngine::class)->start(
    templateCode: 'MEMBERSHIP-APPLICATION',
    subject: $application,
    context: [
        'started_by' => auth()->id(),
        'channel' => 'portal',
    ],
);

use App\Models\User;
use JasperFernandez\Laraflow\Data\TransitionPayload;
use JasperFernandez\Laraflow\Services\WorkflowEngine;

$actor = User::findOrFail(1);

$result = app(WorkflowEngine::class)->apply(
    instance: $instance->fresh(),
    actionCode: 'submit_application',
    actor: $actor,
    payload: new TransitionPayload(
        remarks: 'Submitted for eligibility review',
        metadata: ['channel' => 'portal'],
    ),
);

class User extends Authenticatable
{
    public function hasRole(string $role): bool
    {
        return $this->roles()->where('name', $role)->exists();
    }
}

use App\Models\Application;
use App\Models\User;
use JasperFernandez\Laraflow\Data\TransitionPayload;
use JasperFernandez\Laraflow\Services\WorkflowEngine;

$engine = app(WorkflowEngine::class);

$application = Application::create([
    'name' => 'Jane Doe',
]);

$instance = $engine->start(
    templateCode: 'MEMBERSHIP-APPLICATION',
    subject: $application,
    context: ['started_by' => auth()->id()],
);

$member = User::findOrFail(1);

$firstResult = $engine->apply(
    instance: $instance->fresh(),
    actionCode: 'submit_application',
    actor: $member,
    payload: new TransitionPayload(
        remarks: 'Application submitted',
    ),
);

$reviewer = User::findOrFail(2);

$finalResult = $engine->apply(
    instance: $firstResult->instance->fresh(),
    actionCode: 'approve_application',
    actor: $reviewer,
    payload: new TransitionPayload(
        remarks: 'Application approved',
    ),
);

if ($finalResult->closed) {
    // The workflow has finished.
}

use Illuminate\Database\Eloquent\Model;
use JasperFernandez\Laraflow\Traits\HasWorkflows;

class Application extends Model
{
    use HasWorkflows;
}

// Usage
$application = Application::find(1);
$currentWorkflow = $application->currentWorkflow;
$allWorkflows = $application->workflowInstances;

use JasperFernandez\Laraflow\Events\WorkflowTransitioned;

public function boot()
{
    Event::listen(WorkflowTransitioned::class, function ($event) {
        // $event->result is a TransitionResult DTO
    });
}
bash
php artisan vendor:publish --tag="laraflow-migrations"
php artisan migrate
bash
php artisan vendor:publish --tag="laraflow-config"