Courses

Laravel 12 Multi-Tenancy: All You Need To Know

Bootstrappers: Queued Job Example with Tenant

You're reading a FREE PREVIEW of a PREMIUM course.
Summary of this lesson:
- Implementing queue bootstrappers for tenant-aware jobs
- Handling tenant context in background processes
- Managing notifications across tenant databases

In this lesson, I want to explain the concept of Tenancy Bootstrappers. It doesn't sound very easy, but I will try to make it clear.

It's the class that takes care of the tenancy in various parts of your application in multiple functionalities, like creating the databases and taking care of the cache and the file system. In this lesson, I want to demonstrate the queue Tenancy Bootstrapper because it's pretty easy.

What problem does it solve? For example, if you add a job to your queue that needs to process some data from the database, which database should it query? The main one or the tenant one? Then which tenant?

By default, the job does not contain that code. It would load just the Tenant model and try to do something.

If you enable queue bootstrapper, it will add tenant ID to the jobs and process that when processing the job.

By default, queue bootstrapper is enabled.

config/tenancy.php:

return [
// ...
 
'bootstrappers' => [
Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper::class,
Stancl\Tenancy\Bootstrappers\CacheTenancyBootstrapper::class,
Stancl\Tenancy\Bootstrappers\FilesystemTenancyBootstrapper::class,
Stancl\Tenancy\Bootstrappers\QueueTenancyBootstrapper::class,
// Stancl\Tenancy\Bootstrappers\RedisTenancyBootstrapper::class, // Note: phpredis is needed
],
 
// ...
];

Let's send an email to the administrator when a project is created. First, we need a Notification class.

php artisan make:notification ProjectCreatedNotification

The notification must implement...

The full lesson is only for Premium Members.
Want to access all 22 lessons of this course? (88 min read)

You also get:

  • 76 courses
  • Premium tutorials
  • Access to repositories
  • Private Discord