Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here

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

Premium
3:26

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 of our courses? (29 h 14 min)

You also get:

54 courses
Premium tutorials
Access to repositories
Private Discord
Get Premium for $129/year or $29/month

Already a member? Login here

Comments & Discussion

YD
Yimy David ✓ Link copied!

Hi, how can I configure the filesystem? I am using Spatie multitenancy with subdomains, and I noticed that when I upload and display images, I get this error.

Access to fetch at 'http://project.test/storage/logos/01J1KEC32VKM9YMNDXDG9TPKF3.jpg' from origin 'http://drea.project.test' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.