Overview
TheUpdateMethod attribute marks public methods that combine query and signal behavior. Update methods read the current workflow state and return a value immediately, then conditionally trigger workflow execution if the outbox was consumed during the read.
Basic Usage
How It Works
When you call an update method on aWorkflowStub, the framework:
- Detects the attribute -
WorkflowStubuses reflection to identify methods marked withUpdateMethod(WorkflowStub.php:318-333) - Loads the active workflow - Retrieves the current workflow state (WorkflowStub.php:92)
- Instantiates and queries - Creates workflow instance and calls
query()method (WorkflowStub.php:94-95) - Checks outbox consumption - Determines if the query consumed any outbox messages (WorkflowStub.php:97)
- Conditionally signals - If outbox was consumed, creates a signal and dispatches the workflow (WorkflowStub.php:98-111)
- Returns the result - Returns the query result regardless of whether execution was triggered (WorkflowStub.php:114)
Update vs Query vs Signal
UpdateMethod is a hybrid that combines aspects of both query and signal methods:
| Feature | QueryMethod | UpdateMethod | SignalMethod |
|---|---|---|---|
| Returns value | ✅ Yes | ✅ Yes | ❌ No (void) |
| Triggers execution | ❌ Never | ⚠️ Conditionally | ✅ Always |
| Modifies state | ❌ No | ⚠️ Maybe | ✅ Yes |
| Creates signal record | ❌ No | ⚠️ If needed | ✅ Yes |
| Use case | Read state | Read + consume | Write state |
Outbox Consumption Detection
The key to update methods is theoutboxWasConsumed property. This flag is set when the workflow’s outbox is accessed during the query:
outboxWasConsumed is true, the framework knows that:
- The query consumed a message from the outbox
- The workflow needs to be signaled to generate the next message
- A signal record should be created to replay this consumption
Practical Example: Request-Reply Pattern
Update methods are perfect for request-reply patterns where you want to:- Get the current response (query behavior)
- Signal the workflow to prepare the next response (signal behavior)
Signal Replay
Update methods create signal records just likeSignalMethod. This ensures that during workflow replay, the outbox consumption is replayed correctly:
When to Use UpdateMethod
UseUpdateMethod when you need to:
- Request-reply patterns - Get a response and prepare the next one
- Polling endpoints - Client polls for updates and triggers continuation
- Interactive workflows - User requests data and workflow prepares next step
- Outbox pattern - Consume messages from workflow outbox
- State machines - Get current state and transition if consumed
Detection and Caching
Like query and signal methods, update methods are detected via reflection and cached:WorkflowStub::$updateMethodCache (WorkflowStub.php:48).
Testing Update Methods
When testing, update methods work withWorkflowStub::fake():
Technical Details
- Target: Methods only (
Attribute::TARGET_METHOD) - Return Type: Can return any value (like QueryMethod)
- Execution: Synchronous query + conditional async signal
- Caching: Cached attribute detection for performance (WorkflowStub.php:48)
- Outbox: Relies on
$workflow->outboxWasConsumedflag - Dispatch: Uses
Signal::dispatch()when outbox consumed (WorkflowStub.php:111)
See Also
- SignalMethod - Modify workflow state and trigger execution
- QueryMethod - Read workflow state without modification
- WorkflowStub - Learn about loading and interacting with workflows