Skip to main content
The StoredWorkflow model represents a workflow instance in the database. It stores the workflow’s class, arguments, output, and execution status.

Database Table

Table: workflows

Model Attributes

id
integer
required
Primary key for the workflow instance
class
string
required
Fully qualified class name of the workflow (e.g., App\Workflows\MyWorkflow)
arguments
string
Serialized workflow arguments passed during workflow creation
output
string
Serialized output/result of the workflow execution
status
WorkflowStatus
required
Current status of the workflow. Possible values:
  • pending - Workflow is queued for execution
  • running - Workflow is currently executing
  • completed - Workflow finished successfully
  • failed - Workflow encountered an error
  • continued - Workflow has been continued with ContinueAsNew
Default: pending
created_at
timestamp
required
When the workflow was created (microsecond precision)
updated_at
timestamp
required
When the workflow was last updated (microsecond precision)

Relationships

logs()

Returns all activity execution logs for this workflow, ordered by ID.
public function logs(): HasMany
Returns: HasMany relationship to StoredWorkflowLog

exceptions()

Returns all exceptions that occurred during workflow execution.
public function exceptions(): HasMany
Returns: HasMany relationship to StoredWorkflowException

signals()

Returns all signals sent to this workflow.
public function signals(): HasMany
Returns: HasMany relationship to StoredWorkflowSignal

timers()

Returns all timers created by this workflow.
public function timers(): HasMany
Returns: HasMany relationship to StoredWorkflowTimer

parents()

Returns parent workflows that spawned this workflow as a child.
public function parents(): BelongsToMany
Returns: BelongsToMany relationship to StoredWorkflow Pivot fields:
  • parent_index - The activity index in the parent workflow
  • parent_now - The timestamp when the child was created

children()

Returns child workflows spawned by this workflow.
public function children(): BelongsToMany
Returns: BelongsToMany relationship to StoredWorkflow Pivot fields:
  • parent_index - The activity index in the parent workflow
  • parent_now - The timestamp when the child was created

continuedWorkflows()

Returns workflows that were created via ContinueAsNew from this workflow.
public function continuedWorkflows(): BelongsToMany
Returns: BelongsToMany relationship to StoredWorkflow where parent_index equals StoredWorkflow::CONTINUE_PARENT_INDEX

activeWorkflow()

Returns the current active workflow in a continuation chain.
public function activeWorkflow(): BelongsToMany
Returns: BelongsToMany relationship to StoredWorkflow where parent_index equals StoredWorkflow::ACTIVE_WORKFLOW_INDEX

Important Methods

toWorkflow()

Converts the stored workflow back into a WorkflowStub instance.
public function toWorkflow(): WorkflowStub

workflowMetadata()

Returns the workflow metadata containing arguments and options.
public function workflowMetadata(): WorkflowMetadata

workflowArguments()

Returns the unserialized workflow arguments.
public function workflowArguments(): array

workflowOptions()

Returns the workflow execution options.
public function workflowOptions(): WorkflowOptions

effectiveConnection()

Returns the queue connection that should be used for this workflow.
public function effectiveConnection(): ?string

effectiveQueue()

Returns the queue name that should be used for this workflow.
public function effectiveQueue(): ?string

findLogByIndex()

Finds a log entry by its index number.
public function findLogByIndex(int $index, bool $fresh = false): ?StoredWorkflowLog
Parameters:
  • $index - The activity index to find
  • $fresh - Whether to query the database instead of using loaded relations
Returns: The log entry or null if not found

hasLogByIndex()

Checks if a log entry exists for a given index.
public function hasLogByIndex(int $index): bool

createLog()

Creates a new log entry for this workflow.
public function createLog(array $attributes): StoredWorkflowLog

findTimerByIndex()

Finds a timer by its index number.
public function findTimerByIndex(int $index): ?StoredWorkflowTimer

createTimer()

Creates a new timer for this workflow.
public function createTimer(array $attributes): StoredWorkflowTimer

orderedSignals()

Returns signals ordered by creation time.
public function orderedSignals(): Collection

active()

Returns the currently active workflow instance. If the workflow has been continued, returns the continuation.
public function active(): StoredWorkflow

prunable()

Defines which workflows are eligible for automatic pruning.
public function prunable(): Builder
Workflows are prunable if they:
  • Have status completed
  • Were created before the configured prune_age (default: 1 month)
  • Have no parent workflows

Querying Workflow History

Get all completed workflows

use Workflow\Models\StoredWorkflow;

$completedWorkflows = StoredWorkflow::where('status', 'completed')
    ->orderBy('created_at', 'desc')
    ->get();

Get workflow with all logs

$workflow = StoredWorkflow::with('logs')
    ->find($workflowId);

foreach ($workflow->logs as $log) {
    echo "Activity: {$log->class} at {$log->now}\n";
}

Get workflow with exceptions

$workflow = StoredWorkflow::with('exceptions')
    ->find($workflowId);

if ($workflow->exceptions->isNotEmpty()) {
    foreach ($workflow->exceptions as $exception) {
        echo "Error: {$exception->exception}\n";
    }
}

Get workflows by class

$myWorkflows = StoredWorkflow::where('class', 'App\\Workflows\\ProcessOrder')
    ->where('status', 'running')
    ->get();

Get child workflows

$workflow = StoredWorkflow::with('children')->find($workflowId);

foreach ($workflow->children as $child) {
    echo "Child workflow: {$child->class} (Status: {$child->status})\n";
}

Get workflow continuation chain

$workflow = StoredWorkflow::with('continuedWorkflows')->find($workflowId);

foreach ($workflow->continuedWorkflows as $continuation) {
    echo "Continued as: {$continuation->id}\n";
}

Find the active workflow in a continuation chain

$workflow = StoredWorkflow::find($workflowId);
$activeWorkflow = $workflow->active();

echo "Active workflow ID: {$activeWorkflow->id}\n";
echo "Status: {$activeWorkflow->status}\n";

Configuration

The model class can be customized in config/workflows.php:
'stored_workflow_model' => Workflow\Models\StoredWorkflow::class,

Constants

CONTINUE_PARENT_INDEX
int
Special parent index value (PHP_INT_MAX) used to mark workflow continuation relationships
ACTIVE_WORKFLOW_INDEX
int
Special parent index value (PHP_INT_MAX - 1) used to mark the active workflow in a continuation chain

Traits

  • HasStates - Provides state management functionality for workflow status
  • Prunable - Enables automatic cleanup of old completed workflows

See Also

Build docs developers (and LLMs) love