Laravel

Carry Metadata Across Laravel Jobs with Context

Carry Metadata Across Laravel Jobs with Context

Keep request identifiers available in background jobs.

  • 23 Oct, 2026
  • 1 Views

Carry Metadata Across Laravel Jobs with Context

Imagine a user places an order, and your application dispatches several background jobs to process it.

When one job fails, identifying which request or order triggered it can be difficult.

Laravel's Context feature helps you attach metadata to the current execution and make it available to logs and queued jobs.

Add Context Data

use Illuminate\Support\Facades\Context;

Context::add('request_id', (string) str()->uuid());
Context::add('order_id', $order->id);

You can now retrieve the values elsewhere in the same execution:

$requestId = Context::get('request_id');
$orderId = Context::get('order_id');

Share Context with Queued Jobs

Suppose you dispatch an order-processing job:

Context::add('order_id', $order->id);

ProcessOrder::dispatch($order);

Laravel can automatically carry the current context into the queued job.

Inside the job, you can access it:

use Illuminate\Support\Facades\Context;

public function handle(): void
{
    logger()->info('Processing order', [
        'order_id' => Context::get('order_id'),
    ]);
}

This helps connect background processing with the operation that initiated it.

Add Context to Logs Automatically

Laravel can also share context with log entries:

Context::add('request_id', (string) str()->uuid());

logger()->info('Order processing started');

Contextual values can be included with log records, making it easier to trace related activity.

When Should You Use It?

  • Tracking request IDs
  • Debugging queued jobs
  • Following order-processing workflows
  • Adding tenant or account identifiers to logs
  • Connecting related application events


Share: