PHP code example of mrnewport / laravel-flow

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

    

mrnewport / laravel-flow example snippets


return [
    'user_model' => \App\Models\User::class,
    'rejection_actions' => ['reject','cancel'],
];

FlowManager::reassignStep($stepInstance, $currentUser, $newAssignees);

   $entity = new SomeModel(...);
   $flowInstance = FlowManager::startFlow('start_request',$entity);

   // This creates a FlowInstance row + FlowInstanceStep row
   // If step "start_request" has single_user with user_id=3, it is assigned to user 3.
   

$currentStep = $flowInstance->steps()
    ->whereNull('finished_at')
    ->first();

FlowManager::actionStep($currentStep,'submit');

flow:define-step multi_out --actions="done:stepX,done:stepY,done:stepZ"

FlowManager::actionStep($multiOutStep, 'done');

FlowManager::actionStep($oldStep,'approve');

use MrNewport\LaravelFlow\Traits\Flowable;

class Document extends Model
{
    use Flowable;
}

$doc = Document::create([...]);
$doc->startFlow('start_request');

$current = $doc->currentFlowStep();
if($current) {
    $doc->flowAction('submit');
}

namespace App\FlowAssignments;

use MrNewport\LaravelFlow\Models\FlowInstanceStep;
use MrNewport\LaravelFlow\Models\FlowStepAssignee;
use MrNewport\LaravelFlow\Assignments\AssignmentStrategyInterface;

class SlackChannelStrategy implements AssignmentStrategyInterface
{
    public function __construct(protected array $params=[])
    {
        // e.g. $params['channel'] => '#general'
    }

    public function assign(FlowInstanceStep $instanceStep): void
    {
        // store the Slack channel
        FlowStepAssignee::create([
            'flow_instance_step_id'=>$instanceStep->id,
            'assignee_type'=>'slack_channel',
            'assignee_value'=>$this->params['channel']
        ]);
    }

    public function canReassign(FlowInstanceStep $instanceStep, $currentUser): bool
    {
        // If you have logic for reassigning Slack channels, define here
        return false;
    }

    public function reassign(FlowInstanceStep $instanceStep, array $newAssignees): void
    {
        // Example: update the Slack channel
        $instanceStep->assignees()->delete();
        foreach($newAssignees as $chan){
            FlowStepAssignee::create([
                'flow_instance_step_id'=>$instanceStep->id,
                'assignee_type'=>'slack_channel',
                'assignee_value'=>$chan
            ]);
        }
    }
}

use MrNewport\LaravelFlow\Events\FlowActionEvent;

Event::listen(FlowActionEvent::class, function(FlowActionEvent $e){
  if($e->oldStep->step_id==='manager_review' && $e->action==='approve') {
    // E.g. log something or call external API
  }
});
bash
   php artisan vendor:publish --provider="MrNewport\\LaravelFlow\\FlowServiceProvider" --tag=flow-config
   php artisan vendor:publish --provider="MrNewport\\LaravelFlow\\FlowServiceProvider" --tag=flow-stubs
   
bash
   php artisan migrate
   
bash
   php artisan flow:define-step start_request "Start Request" --strategy=single_user --params='{"user_id":3}'
   
bash
   php artisan flow:define-step start_request --actions="submit:manager_review"
   
bash
php artisan flow:define-step manager_review "Manager Review" \
    --notify=true \
    --strategy=multi_user \
    --params='{"user_ids":[3,5,8]}' \
    --actions="approve:director_approval,reject:start_request"