You know which tenant a request belongs to in the middleware. Then the request writes a log line, dispatches a job, and the job writes more log lines on a worker with no idea who the tenant was. People usually fix this by adding $tenantId to every job constructor and every Log::info() call. Laravel's Context facade does it for you: set the value once, and it is attached to every log entry and carried into every job you dispatch.
The quick answer
use Illuminate\Support\Facades\Context ;
Context :: add ( 'tenant_id' , 42 ); // in middleware, once per request
// anywhere later, including inside a queued job: $tenantId = Context :: get ( 'tenant_id' ); // 42
Enter fullscreen mode
Exit fullscreen mode
Every log entry written after that carries tenant_id as metadata, and every job dispatched after that carries it to the worker.
Step 1: set it in middleware
<?php
namespace App\Http\Middleware ;
use Closure ; use Illuminate\Http\Request ; use Illuminate\Support\Facades\Context ; use…