How to make your Laravel queues tenant aware
Here’s a concise summary: In single-database multi-tenancy setups, handling queues can be challenging. Passing the tenant ID into every job constructor is tedious and leads to repetitive tenant filters in queries, even when global scopes are used. A cleaner approach is to make tenant-aware jobs implement a special interface and handle tenancy at the queue level. By listening to queue events in a ServiceProvider, the current tenant (e.g., domain) is injected into the job payload when it’s queued, and then restored when the job is processed. This centralizes tenant initialization and avoids repetitive, error-prone code inside individual jobs.
09 Jul, 2021 00:00 | 3 min to read