In Laravel 12, we define global middleware for web requests using the withMiddleware method in bootstrap/app.php, which makes it super easy to apply middleware to all HTTP requests automatically. I’m looking to do something similar for queued jobs: I want a specific middleware to run for all ShouldQueue entities—custom jobs, notifications, queued events, queued mailables, etc.—without having to attach it to each class individually.
Here’s the context: I’m building a multi-tenant app where every Notifiable (like a User) has a tenant_id. I’ve created a middleware (ConfigureJobTenantMiddleware) that sets tenant-specific configurations (e.g., mail settings, app URL) based on this tenant_id. Right now, I can apply it to custom jobs by defining a middleware() method, but I want it to work universally, including for Laravel’s built-in queueables like SendQueuedNotifications, without modifying every class.
Any insights or best practices for Laravel 12 would be awesome—especially if there’s a cleaner way to do this without hacking too deep into the framework!
I know there are many Tenancy packages but I want to implement mine to learn and understand Laravel as a beginner.
Thanks!
In Laravel 12, we define global middleware for web requests using the withMiddleware method in bootstrap/app.php, which makes it super easy to apply middleware to all HTTP requests automatically. I’m looking to do something similar for queued jobs: I want a specific middleware to run for all ShouldQueue entities—custom jobs, notifications, queued events, queued mailables, etc.—without having to attach it to each class individually.
Here’s the context: I’m building a multi-tenant app where every Notifiable (like a User) has a tenant_id. I’ve created a middleware (ConfigureJobTenantMiddleware) that sets tenant-specific configurations (e.g., mail settings, app URL) based on this tenant_id. Right now, I can apply it to custom jobs by defining a middleware() method, but I want it to work universally, including for Laravel’s built-in queueables like SendQueuedNotifications, without modifying every class.
Any insights or best practices for Laravel 12 would be awesome—especially if there’s a cleaner way to do this without hacking too deep into the framework!
I know there are many Tenancy packages but I want to implement mine to learn and understand Laravel as a beginner.
Thanks!
Share Improve this question asked Mar 30 at 17:04 Joe NyamburaJoe Nyambura 2035 silver badges14 bronze badges1 Answer
Reset to default 2The Queue
facade has a createPayloadUsing
method which allows you to register a callback that is executed when the job payload is created. You should be able to use this to inject middleware into your jobs.
The code below injects a middleware (ConfigureJobTenantMiddleware
) into the job and makes sure it runs before the middleware defined by the job itself.
use Illuminate\Support\Facades\Queue;
use Illuminate\Contracts\Queue\ShouldQueue;
Queue::createPayloadUsing(function ($connection, $queue, $payload) {
if (isset($payload['data']['command'])) {
$job = unserialize($payload['data']['command']);
if ($job instanceof ShouldQueue) {
$middleware = [new ConfigureJobTenantMiddleware()];
if (method_exists($job, 'middleware')) {
$middleware = array_merge($middleware, $job->middleware());
}
$job->middleware = $middleware;
$payload['data']['command'] = serialize($job);
}
}
return $payload;
});