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 project2cd project3composer require laravel/breeze --dev4php 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.
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/tenancy2php artisan tenancy:install3php 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...