Courses

Laravel 12 Multi-Tenancy: All You Need To Know

archtechx / tenancy: Installation, Configuration and Register Tenant

You're reading a FREE PREVIEW of a PREMIUM course.
Summary of this lesson:
- Installing and configuring stancl/tenancy package
- Setting up central and tenant routes
- Configuring single-database architecture
- Implementing tenant registration with subdomains

Video Version of the Lesson

[Only for premium members]

Link to the repository

[Only for premium members]

The next package we will check is stancl/tenancy. With this package, you can have single and multi-database tenancy. First, we will take a look at a single database approach.

The starting point is the same CRUD with the Project and Task without any tenancy.


Installation and Configuration

So, first, install the package via composer and then run the tenancy:install command.

composer require stancl/tenancy
php artisan tenancy:install
php artisan migrate

Next, we must add the TenancyServiceProvider.

bootstrap/providers.php:

return [
App\Providers\AppServiceProvider::class,
App\Providers\TenancyServiceProvider::class,
];

Install command created the Migration for the tenants table. Now we must make a Model for that table and replace the content of the Model with the code from the quickstart.

php artisan make:model Tenant

app/Models/Tenant.php:

namespace App\Models;
 
use Stancl\Tenancy\Database\Models\Tenant as BaseTenant;
use Stancl\Tenancy\Contracts\TenantWithDatabase;
use Stancl\Tenancy\Database\Concerns\HasDatabase;
use Stancl\Tenancy\Database\Concerns\HasDomains;
 
class Tenant extends BaseTenant implements TenantWithDatabase
{
use HasDatabase, HasDomains;
}

Next, in the config/tenancy.php, we need to set that package to use...

The full lesson is only for Premium Members.
Want to access all 21 video+text lessons of this course? (1 h 21 min)

You also get:

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