Laravel Context: How to Share Execution Data Across Requests, Logs, and Jobs
Laravel's Context feature allows developers to store and retrieve information — such as trace IDs, tenant IDs, or URLs — that needs to persist throughout a single application execution without being passed explicitly between methods. The data can be added via Context::add() and accessed anywhere in the application using Context::get(), making it especially useful for consistent log enrichment. When a queued job is dispatched, Laravel automatically captures the current context, serializes it into the job payload, and restores it when the worker processes the job — a process called dehydration and hydration. This ensures that contextual data from an originating HTTP request, like a trace ID, remains accessible inside the job for debugging distributed workflows. Importantly, Laravel isolates context per job execution, preventing data from one job from leaking into the next in long-running queue workers.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.


Discussion (0)
Log in to join the discussion and vote.
Log in