Build a Laravel 12 SaaS Project with Tenancy for Laravel — Beginner's Guide

Author

Kritim Yantra

Apr 27, 2025

Build a Laravel 12 SaaS Project with Tenancy for Laravel — Beginner's Guide

Have you ever dreamed of creating your own SaaS (Software as a Service) platform where each user or company gets their own mini-application?

Good news — Laravel 12 and the powerful Tenancy for Laravel package make it easier than ever!

In this complete beginner’s guide, you’ll learn step-by-step how to:

  • Set up a fresh Laravel 12 app
  • Install and configure multi-tenancy
  • Separate central (landlord) and tenant (client) routes
  • Implement important security measures
    ...and a lot more!

Let's start building your first multi-tenant SaaS app! 🚀


🌟 1. Setting Up Laravel 12

Before starting, make sure you have installed:

  • PHP 8.2+
  • Composer (dependency manager for PHP)
  • MySQL (or another database)
  • Node.js and npm (for frontend assets)

Step 1: Install Laravel Installer (optional but helpful)

composer global require laravel/installer

This lets you create new Laravel projects easily using the laravel new command.

Step 2: Create a Fresh Laravel 12 Project

composer create-project --prefer-dist laravel/laravel:^12.0 saas-project
cd saas-project
npm install && npm run build

This will set up Laravel and build the frontend assets!

Step 3: Configure Environment (.env)

Update your .env file to connect to your database:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=saas_project
DB_USERNAME=root
DB_PASSWORD=

🔥 2. Installing Tenancy for Laravel

Now, let’s add multi-tenant magic to our project.

Step 1: Install the Package

composer require stancl/tenancy

This package will handle tenant creation, database isolation, domain mapping, and more!

Step 2: Publish Tenancy Files

php artisan tenancy:install

This will generate:

  • Tenant migrations
  • config/tenancy.php configuration file
  • A separate routes/tenant.php
  • TenancyServiceProvider

Step 3: Run Database Migrations

php artisan migrate

This will create the tables needed to manage tenants centrally.


️ 3. Configuring Tenancy for Laravel

3.1 Customize Tenant Model (Optional)

If you want to extend the tenant model:

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;
}

Then update config/tenancy.php:

'tenant_model' => App\Models\Tenant::class,

✅ Now you can fully control tenant behavior!


3.2 Define Central (Landlord) Domains

Inside config/tenancy.php, configure:

'central_domains' => [
    'saas.test',
    'localhost',
    '127.0.0.1',
],

📝 Central domains are where your landlord/admin panel will be hosted.


3.3 Customize Middleware on Failures

If tenancy fails to identify a tenant, you can redirect:

\Stancl\Tenancy\Middleware\InitializeTenancyByDomain::$onFail = function () {
    return redirect('https://saas.test/');
};

Place this inside TenancyServiceProvider under the boot() method.


🔀 4. Segregating Central and Tenant Routes

Keeping routes organized is very important in a SaaS project!

4.1 Central (Landlord) Routes — routes/web.php

foreach (config('tenancy.central_domains') as $domain) {
    Route::domain($domain)->group(function () {
        Route::get('/', [LandingPageController::class, 'index']);
        // other central routes...
    });
}

All admin-related pages like login, billing, settings, etc., should be here.


4.2 Tenant (Client) Routes — routes/tenant.php

Route::get('/', function () {
    return 'Welcome Tenant ID: ' . tenant('id');
});

🛡️ These routes will automatically connect to the tenant’s database!

Laravel automatically applies web and tenancy middleware to routes/tenant.php.


🗂️ 5. Using Separate Route Files

Laravel Tenancy already separates your routes:

  • Central routesroutes/web.php
  • Tenant routesroutes/tenant.php

✅ This makes your code clean, scalable, and easy to manage.


🔒 6. Security Best Practices

Security is everything in multi-tenant systems. Here are simple but powerful practices:

6.1 Block Wrong Route Access

Use Laravel's middleware to prevent accessing tenant routes from central domains and vice versa.
Ensure these middleware are added in Http/Kernel.php:

  • PreventAccessFromCentralDomains
  • PreventAccessFromTenantDomains

6.2 Data Isolation (Automatic)

Thanks to Tenancy, every tenant gets:

  • Their own separate database (if using database per tenant)
  • Isolated data

Tip: Always double-check you’re inside the correct tenant context (tenant() helper).


6.3 Secure Authentication Redirects

After login, redirect users to their tenant domain:

return redirect()->to(tenant()->domains->first()->domain . '/dashboard');

This ensures they always land on their own "mini-application".


6.4 Extra Protection Tips

  • Always use HTTPS (SSL certificates).
  • Set up rate limiting to protect tenants from abuse.
  • Implement policies and gates based on the authenticated tenant.
  • Keep your system updated with latest Laravel security patches.

🎯 Conclusion: You Just Built a SaaS Foundation!

By following these steps:

  • You installed Laravel 12.
  • You added tenancy support.
  • You properly separated tenant and central logic.
  • You applied solid security practices.

From here, you can extend your SaaS project by:

  • Adding billing with Laravel Cashier.
  • Allowing subdomain creation for tenants.
  • Creating custom tenant onboarding flows.
  • Building subscription and pricing plans.

The possibilities are endless! 🚀


If you found this guide helpful, share it with your developer friends and start building your SaaS dreams today!
Happy coding! ❤️

Tags

Comments

No comments yet. Be the first to comment!

Please log in to post a comment:

Sign in with Google

Related Posts