Kritim Yantra
Apr 27, 2025
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:
Let's start building your first multi-tenant SaaS app! 🚀
Before starting, make sure you have installed:
composer global require laravel/installer
This lets you create new Laravel projects easily using the laravel new
command.
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!
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=
Now, let’s add multi-tenant magic to our project.
composer require stancl/tenancy
This package will handle tenant creation, database isolation, domain mapping, and more!
php artisan tenancy:install
This will generate:
config/tenancy.php
configuration fileroutes/tenant.php
TenancyServiceProvider
php artisan migrate
This will create the tables needed to manage tenants centrally.
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!
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.
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.
Keeping routes organized is very important in a SaaS project!
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.
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
.
Laravel Tenancy already separates your routes:
routes/web.php
routes/tenant.php
✅ This makes your code clean, scalable, and easy to manage.
Security is everything in multi-tenant systems. Here are simple but powerful practices:
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
Thanks to Tenancy, every tenant gets:
Tip: Always double-check you’re inside the correct tenant context (tenant()
helper).
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".
By following these steps:
From here, you can extend your SaaS project by:
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! ❤️
Transform from beginner to Laravel expert with our personalized Coaching Class starting June 19, 2025. Limited enrollment ensures focused attention.
1-hour personalized coaching
Build portfolio applications
Industry-standard techniques
Interview prep & job guidance
Complete your application to secure your spot
Thank you for your interest in our Laravel mentorship program. We'll contact you within 24 hours with next steps.
No comments yet. Be the first to comment!
Please log in to post a comment:
Sign in with Google