Use the Context facade to capture request metadata that automatically propagates to logs, exception reports, and queued jobs.
When an HTTP request dispatches asynchronous queue jobs, contextual details (like the requesting user ID, tenant ID, or trace UUID) are traditionally lost inside background workers.
Laravel's Context facade stores cross-cutting metadata that automatically flows into logs and queued jobs.
Capturing Context in Middleware
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Context;
use Illuminate\Support\Str;
class CaptureRequestContext
{
public function handle(Request $request, Closure $next)
{
// Add metadata to active Context
Context::add('trace_id', (string) Str::uuid());
Context::add('user_id', $request->user()?->id);
Context::add('ip', $request->ip());
return $next($request);
}
}
Automatic Propagation to Queued Jobs and Logs
When jobs are dispatched during this request, their execution in worker processes automatically retains the context:
// Inside a background job:
Log::info('Generating PDF export.');
// Log entry automatically contains:
// {"trace_id":"8a9f...", "user_id":42, "ip":"127.0.0.1"}
Retrieving and Checking Context Values
$traceId = Context::get('trace_id');
$hasUser = Context::has('user_id');
Summary
- Propagates metadata across HTTP requests, logging engines, and queued jobs.
- Enables end-to-end distributed transaction tracing.
- Native to modern Laravel with zero configuration overhead.
Related Tips
View all tips →Process Improvements in Laravel 13.26: Idle Timeouts and Testing Assertions
Laravel 13.26 introduces ProcessIdleTimedOutException to distinguish idle processes from overall runtime timeouts, alongside new testing assertions for execution count and sequence order.
Eliminate Cache Stampedes with Cache::flexible() and Stale-While-Revalidate
Use Cache::flexible() to serve slightly stale cache values immediately to users while refreshing the cache in the background.