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

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...

The full tutorial [9 mins, 1748 words] is only for Premium Members

Login Or Become a Premium Member for $129/year or $29/month
What else you will get:
  • 58 courses (1054 lessons, total 46 h 42 min)
  • 78 long-form tutorials (one new every week)
  • access to project repositories
  • access to private Discord

Recent Premium Tutorials