Courses

Laravel 12 Multi-Tenancy: All You Need To Know

Multiple DBs: spatie / multi-tenancy

You're reading a FREE PREVIEW of a PREMIUM course.
Summary of this lesson:
- Configuring landlord (central) and tenant database connections
- Organizing migrations into landlord/tenant folders
- Auto-creating tenant databases during registration
- Implementing database switching with tenant middleware

Now let's try to perform a multi-database setup with another package spatie/multi-tenancy. I will remind you, it has fewer features. Its philosophy is to provide the bare bones for the multi-tenancy, but not more than that. Again, we start with a project and task CRUD.


Installation and Configuration

First, we start with a package install via composer and publish the configuration.

composer require spatie/laravel-multitenancy
php artisan vendor:publish --provider="Spatie\Multitenancy\MultitenancyServiceProvider" --tag="multitenancy-config"

The next step is choosing the multiple databases.

First, we must configure the database connections. This will slightly differ from the previous package in configuring database connections.

You will need two connections: landlord and tenant. If we look at the config/multitenancy.php, the database connection names are null for tenant and landlord.

config/multitenancy.php:

return [
// ...
 
'tenant_database_connection_name' => null,
'landlord_database_connection_name' => null,
 
// ...
];

We can create a custom connection for the landlord or use...

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