Courses

Laravel 12 Multi-Tenancy: All You Need To Know

spatie / multi-tenancy: Bare Essentials for Multi-Tenancy

Summary of this lesson:
- Understanding Spatie's minimal approach
- Basic package setup for single database
- Implementing DomainTenantFinder
- Setting up tenant registration with domains

The last package we will cover in the multi-tenancy single database comes from the well-known company Spatie. It has a name of spatie/laravel-multitenancy and has a slightly different philosophy than others. The key philosophy of this package is that it should provide only the bare essentials to enable multi-tenancy and to make your application tenant aware.

What does that mean? The package will provide the minimum functionality for identifying the tenant and switching between them. Still, it will leave your custom application logic to your custom application and will provide only what it should.

In other words, this package has fewer features than we covered previously, especially if we're talking about a single database application. It has quite a lot to offer for multi-database, and we will cover that in the following lessons.

Let's install it in our single database and use roughly the same logic for creating tenants and protecting our data for projects and tasks.


So, as usual, we install the package via composer and then publish the configuration file.

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

The next step is to choose which database type we will use, single or multi-database. This lesson will use the single database. First, we must publish the migrations.

php artisan vendor:publish --provider="Spatie\Multitenancy\MultitenancyServiceProvider" --tag="multitenancy-migrations"
php artisan migrate --path=database/migrations/landlord

The package offers a system of classes of tenant finders. It may be a domain tenant finder or your custom tenant finder.

How do we determine the current tenant? In the config/multitenancy.php, the tenant_finder is set to null. Let's set...

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

You also get:

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