Skip to main content

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

Read more here
Premium Members Only
Join to unlock this tutorial and all of our courses.
Tutorial Premium Tutorial

Laravel Multi-Tenancy with Multi-Database: Step-by-Step Example

February 07, 2023
9 min read

Tutorial last revisioned on April 10, 2024 with Laravel 11

The term "multi-tenancy" has different meanings and implementations in Laravel. In this article, let's take a look at a multi-database approach, using the package stancl/tenancy: I will show you step-by-step, how to make it work.

This is a text-form excerpt from one of the sections of my 2-hour video course: Laravel Multi-Tenancy: All You Need To Know


Initial Laravel Project

Before starting everything about multi-tenancy, let's set up our project very quickly.

1laravel new project
2cd project
3composer require laravel/breeze --dev
4php artisan breeze:install

This is straightforward: install the Laravel project and then install Laravel Breeze for quick authentication scaffolding.

Next, we will have two basic CRUDs:

  • Project (string: name)
  • Task (string: name, foreignId: project_id)

You can see wow those CRUDs are set up here in the GitHub repository.

default breeze with cruds


Tenancy Installation and Configuration

We will use the stancl/tenancy package for managing multi-tenancy. Installation is the same as with any other Laravel package:

1composer require stancl/tenancy
2php artisan tenancy:install
3php artisan migrate

After installing the package, we need to register TenancyServiceProvider in the config/app.php file. After RouteServiceProvider add a line:

config/app.php:

1return [
2 //
3 'providers' => [
4 App\Providers\EventServiceProvider::class,
5 App\Providers\RouteServiceProvider::class,
6 App\Providers\TenancyServiceProvider::class,
7 //
8 ],
9 //
10];

Next, the package created migration for the Tenant modal, but we need to...

Premium Members Only

This advanced tutorial is available exclusively to Laravel Daily Premium members.

Premium membership includes:

Access to all premium tutorials
Video and Text Courses
Private Discord Channel

Comments & Discussion

MS
mahdi sahib ✓ Link copied!

thank you! that is help alot, looking for the same article but using jetstream please.

PK
Povilas Korop ✓ Link copied!

I think for Jetstream it would be mostly the same, with different Controllers, like RegisteredUserController in Jetstream would probably be Fortify Action CreateNewUser, from what I remember. But all the other Model/route/middleware structure would be the same as this article.

K
kaleemullah ✓ Link copied!

Hello Everyone ! which one should i use for api sanctum or passport in laravel multi tenancy ? below subdomains are user1.domain.com user2.domain.com database is centeral for all users.

Thanks

M
Modestas ✓ Link copied!

Hi, there is no single answer for this. Both API authentication methods are different, so without knowing all the specifics.

Here's a few guides to help you make a decision:

https://laraveldaily.com/video/laravel-api-auth-demo-passport-oauth-and-sanctum

https://laraveldaily.com/lesson/laravel-api/authentication-sanctum-passport-jwt

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.