Skip to main content

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 the Illuminate\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 a trace_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.
After this setup, logs written from your controllers and services will automatically be tagged with 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

The when 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. Using push, values are appended in the order they are added.
Here’s an example of recording query execution history on a stack:

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.
To pop the last value off a stack, use 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

Use forget to remove keys.

Scoped context

The scope 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.
If you modify an object inside the scope, that change is reflected outside the scope as well. There’s no problem with primitive values.

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 ordinary get method—only via dedicated methods like getHidden.
Hidden Context has the same set of methods as regular context.

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 the trace_id you attached during the request automatically flows through to queue-side logs.
You can see that the request-time trace_id also appears in the queue-side log.

Dehydrating — customize on job send

Use Context::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.
Inside a dehydrating callback, do not use the Context facade; only operate on the $context repository passed to the callback. Using the facade would modify the current process’s context.

Hydrated — restore on job execution

Use Context::hydrated to add processing right when the context is restored just before a job runs. For example, apply a saved locale to your configuration.
Inside a hydrated callback, do not use the Context facade either; only operate on the $context repository passed in.

Summary

Because Hidden Context is not written to logs, you can safely store the following types of data:
  • Session IDs or user IDs (when you don’t want them in logs)
  • API keys or authentication tokens
  • Locales or configuration values (that you want to propagate to queues but not log)
  • Internal flags or state
  1. Propagating locale: Save app.locale into Hidden Context in dehydrating, then Config::set in hydrated to restore.
  2. Propagating authentication info: Make the authenticated user’s information from the request available inside queue jobs.
  3. Tenant ID: In multi-tenant applications, share the tenant identifier across queues.
Last modified on July 13, 2026