Back to Course |
Laravel 11 Multi-Tenancy: All You Need To Know

Quick Run Through More Extra Features

Finally, in the last lesson of this section, I will quickly run through other features of this package.


So, what can you separate databases for different tenants, what else can you separate? For example, you can separate the cache. If you use a cache driver with tags like Redis, you can separate the tags and clear the cache of certain tenants.


Then, a complex file system tenancy bootstrapper enables tenants to separate storage or assets. And, as it is said in the official documentation, this bootstrapper is the most complex one by far because it has a lot of logic on how to separate by subfolders.

What it does is separate the files. For example, if I store that in the path of tasks, it separates create storage/app. Instead of storage/app, it has storage/{tenant_id}/app, and each tenant has its subfolder with storage inside.

Then, you could separate the tenant and, for example, backup only that tenant's files if needed.


Also, this package allows testing to be tenantable. For example, you can run the test on the tenant app. On the setUp(), you need to initialize tenancy.

use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
 
abstract class TestCase extends BaseTestCase
{
protected $tenancy = false;
 
public function setUp(): void
{
parent::setUp();
 
if ($this->tenancy) {
$this->initializeTenancy();
}
}
 
public function initializeTenancy()
{
$tenant = Tenant::create();
 
tenancy()->initialize($tenant);
}
}

And then, in your test cases, you can set the $tenancy property to true. That is quite helpful.

class FooTest extends TestCase
{
protected $tenancy = true;
 
/** @test */
public function some_test()
{
$this->assertTrue(...);
}
}

Finally, I want to mention the integration with other packages. Generally, if you dive deeper into multi-tenancy and then add more functionality, every new feature needs to be aware of the tenant: which tenant to work with, which database to work with, where to get assets from or storage file or something else or cache or queues.

So, all the external packages don't know about your multi-tenancy. For some packages, the tenancy package advises how to get them to work together. For example, for spatie/laravel-activitylog you need to change some config.

For spatie/laravel-permission, you need to create the permission tables, move them from database/migrations to database/migrations/tenant, so they would be created in each of the tenants separately.

That is the logic for more packages because then you need to move the migrations into the tenant and migrate in each database separately.

There are more packages mentioned here, such as Passport, Nova, and others. Generally, if you go the complex route of multi-database multi-tenancy, it opens up a deep rabbit hole, Pandora's box, or whatever you call it.

And with each new functionality, you need to be aware of tenants, databases, subdomains, etc.


For now, for this course, you've got the basics of what you need to know: how multi-tenancy works, how to set it up, and how to configure it. From here, it's a matter of your fantasy and skills to apply to your projects.

Good luck with that. And see you guys in other lessons!