What is Context
Laravel’s Context feature is a mechanism for recording and sharing information across requests, queue jobs, and command executions. When you add information through theIlluminate\Support\Facades\Context facade, that information is automatically attached to every log entry your application writes.
This lets you clearly distinguish between data passed to individual log calls and shared information held by Context.
It’s especially useful for tracing in distributed systems or queue-based architectures.
Context propagation flow
Basic usage
The most typical usage is to set atrace_id in middleware. It will automatically be included in every subsequent log entry.
1
Create the middleware
2
Add a trace ID to Context
3
Register the middleware
Register it as global middleware in
bootstrap/app.php.url and trace_id.
Writing to context
add — add a value
add overwrites existing keys. Use addIf to add only when the key does not already exist.
increment / decrement — manage counters
Dedicated methods for incrementing or decrementing numbers. Specify the change amount as the second argument.when — add conditionally
Thewhen method lets you add different data depending on whether a condition is true or false.
push — add to a stack
Context supports “stacks” that hold list-style data. Usingpush, values are appended in the order they are added.
Retrieving context
get / all
only / except — retrieve a subset
pull / pop — retrieve and remove
pull retrieves a key’s value and removes it from the context at the same time.
pop.
remember — set and return if missing
has / missing — check whether a key exists
has returns true even if null is stored. It only checks whether the key is registered.Removing context
Useforget to remove keys.
Scoped context
Thescope method lets you temporarily change context only while a closure runs, and automatically restores the previous state afterward.
It’s useful for testing or when you want to include temporary additional information in logs for a specific local operation.
Hidden Context
Data you don’t want to appear in logs (passwords, API keys, PII, etc.) should be stored in Hidden Context. It cannot be retrieved via the ordinaryget method—only via dedicated methods like getHidden.
Propagating to queue jobs
When you dispatch a job to a queue, the current context is automatically serialized into the job’s payload. When the job runs, the original context is restored, so thetrace_id you attached during the request automatically flows through to queue-side logs.
trace_id also appears in the queue-side log.
Dehydrating — customize on job send
UseContext::dehydrating to transform the context immediately before a job is sent.
For example, use it when you want to pass the locale determined by the Accept-Language header to the queue.
Hydrated — restore on job execution
UseContext::hydrated to add processing right when the context is restored just before a job runs.
For example, apply a saved locale to your configuration.
Summary
Context vs Log::withContext
Context vs Log::withContext
Common Dehydrate/Hydrate use patterns
Common Dehydrate/Hydrate use patterns
- Propagating locale: Save
app.localeinto Hidden Context indehydrating, thenConfig::setinhydratedto restore. - Propagating authentication info: Make the authenticated user’s information from the request available inside queue jobs.
- Tenant ID: In multi-tenant applications, share the tenant identifier across queues.