Overview
TheQueryMethod attribute marks public methods in your workflow that read the current state without modifying it or triggering workflow execution. Query methods provide a synchronous way to inspect workflow state at any time.
Basic Usage
How It Works
When you call a query method on aWorkflowStub, the framework:
- Detects the attribute -
WorkflowStubuses reflection to identify methods marked withQueryMethod(WorkflowStub.php:352-366) - Loads the active workflow - Retrieves the current workflow state from the database (WorkflowStub.php:85)
- Instantiates the workflow - Creates a new workflow instance with the current state (WorkflowStub.php:87)
- Calls the query method - Executes the method and returns the result immediately (WorkflowStub.php:88)
- No execution triggered - The workflow does NOT resume or dispatch any jobs
Query Methods Return Values
Unlike signal methods (which return void), query methods can return any value:Querying Workflow State
Query methods are called synchronously and return immediately:When to Use QueryMethod
UseQueryMethod when you need to:
- Check workflow status - Inspect progress without affecting execution
- Read computed state - Get calculated values based on workflow properties
- Monitor progress - Track workflow advancement for dashboards or UI
- Debug workflows - Inspect internal state during development
- Conditional logic - Make decisions based on workflow state
Query Methods are Read-Only
Query methods should NOT modify workflow state. While you technically can modify state in a query method, the changes won’t be persisted and will cause unpredictable behavior:Complex Query Logic
Query methods can perform complex calculations or aggregations:Detection and Caching
WorkflowStub caches query method detection for performance:Differences from SignalMethod and UpdateMethod
- QueryMethod: Reads state only, returns immediately, no execution triggered
- SignalMethod: Modifies state, triggers execution, returns void
- UpdateMethod: Reads current state AND queues execution if state was modified
Technical Details
- Target: Methods only (
Attribute::TARGET_METHOD) - Return Type: Can return any value (unlike SignalMethod which returns void)
- Execution: Synchronous, no job dispatched
- State: Workflow is instantiated fresh for each query
- Performance: Cached attribute detection, direct method invocation
See Also
- SignalMethod - Modify workflow state and trigger execution
- UpdateMethod - Read state and conditionally trigger execution
- WorkflowStub - Learn about loading and interacting with workflows