Skip to content

Instantly share code, notes, and snippets.

@danclaytondev
Last active May 7, 2021 13:54
Show Gist options
  • Save danclaytondev/bbb923f6db1f550761251dccce1048eb to your computer and use it in GitHub Desktop.
Save danclaytondev/bbb923f6db1f550761251dccce1048eb to your computer and use it in GitHub Desktop.
Laravel Workflows using models
<?php
// This isn't quite the same as the production code, but I wanted to be clear in this demo!
class LoanApplication extends Model
{
public function getNextStage() ?WorkflowStage
{
$nextSequence = $this->workflow->sequences()
->where('sequence_number', $this->current_sequence_number + 1)
->first();
if(is_null($nextSequence) {
return null;
} else {
return $nextSequence->stage;
}
}
public function workflow()
{
return $this->belongsTo(Workflow::class);
}
}
class Workflow extends Model
{
public function sequences()
{
return $this->hasMany(WorkflowSequences::class);
}
}
class WorkflowSequence extends Model
{
public function stage()
{
return $this->belongsTo(WorkflowStage::class, 'workflow_stage_id);
}
}

Models

Workflow:

  • id
  • name

WorkflowStage:

  • id
  • name

WorkflowSequence:

  • id
  • workflow_id
  • workflow_stage_id
  • sequence_number

YourItem (say, Loan Application):

  • your other stuff
  • workflow_id
  • current_sequence_number
  • could also cache the current stage here, but not necessary

Example of stages

id name
1 Applied
47 In Review
98 Awaiting References

(the order of this doesnt matter)

Example of sequences, assume we have a workflow with id 1 "basic application process"

id workflow_id workflow_stage_id sequence_number
1 1 1 1
2 1 98 2
3 1 47 3

Then we have a LoanApplication which is "Awaiting References" with:

  • workflow_id: 1
  • current_sequence_number: 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment