Building Multi-Tenant SaaS in PHP: Lessons From Production

Building Multi-Tenant SaaS in PHP: Lessons From Production

An apartment building works because every family shares the roof, the lifts and the water supply, yet nobody can walk into a neighbour’s flat. The moment one key opens two doors, trust in the whole building disappears. Laravel multi tenancy follows the same rule: tenants share infrastructure to keep costs low, but their data must stay behind separate locks. Teams that specialise in Laravel SaaS development spend more time on those locks than on almost anything else in the product.

For most SaaS products, a single database with a tenant ID on every row is the right starting point: it is cheaper and simpler to run. Separate databases per tenant make sense for enterprise or regulated customers who need stronger isolation. 

This article compares the models, explains how to isolate tenant data, and shares lessons from running multi-tenant Laravel apps in production.

The guidance reflects how Acquaint Softtech builds SaaS platforms as an ISO 27001-certified Official Laravel Partner, drawing on 1,300+ projects and 70+ in-house engineers serving clients in the USA, UK, Europe, and Australia.

Single or multi-database for SaaS?

Choose a single database when you expect many small or mid-sized tenants and want simple operations. Choose a database per tenant when customers demand strict isolation, custom backups, data residency, or contractual separation.

Why the choice matters early

Switching tenancy models after launch means migrating every tenant’s data, so decide before the first paying customer. Many teams start with a single database and offer dedicated databases later as a premium tier.

Three tenancy models compared

ModelStrengthTrade-off
Single database, tenant columnCheapest, simplest to runIsolation relies on code
Single database, schema per tenantStronger separation, one serverPostgreSQL only, more migrations
Database per tenantStrongest isolation, easy exportHigher cost and operational load

Packages that help

Tenancy for Laravel (stancl/tenancy) and Spatie’s laravel-multitenancy handle tenant identification, database switching, and tenant-aware bootstrapping, so you do not need to build that layer from scratch.

How to isolate tenant data

In a single database, isolate data by scoping every query to the current tenant automatically, never by remembering to add a where clause. The safest pattern is a global Eloquent scope applied through a shared trait on every tenant-owned model.

Read Also: News JotechGeeks Latest Technology News and Updates

Layers of protection

  • Global scopes: filter every query by tenant ID by default.
  • Automatic assignment: set the tenant ID when records are created, not in each controller.
  • Policies: check tenant ownership again in authorisation rules.
  • Database rules: on PostgreSQL, row-level security adds a final safety net.
  • Tests: write tests that try to read another tenant’s data and expect failure.

Watch raw queries

Global scopes do not cover raw SQL or the query builder used without models. Review those paths carefully, because that is where most cross-tenant leaks happen.

Identifying the tenant

Every request must resolve the tenant before any data loads. Common approaches are a subdomain (acme.yourapp.com), a custom domain, a path segment, or the logged-in user’s account.

Do it once, in middleware

Resolve the tenant in middleware, store it in a single place in the container, and make every other part of the app read from there. Providers of Laravel development services with SaaS experience treat this as core architecture, not a helper function.

Tenant-aware queues, cache and files

The request is not the only place tenant context matters. Background jobs, cached data, and uploaded files all need it too.

AreaRiskFix
Queued jobsJob runs without tenant contextStore tenant ID in the job payload
CacheOne tenant reads another’s cachePrefix keys by tenant
File storageShared paths expose filesTenant folders and signed URLs
Scheduled tasksRuns once, not per tenantLoop tenants explicitly

Migrations and operations at scale

With one database, a migration runs once. With a database per tenant, it runs hundreds or thousands of times, and some will fail. Plan for that from the start.

What to build in

  • Run tenant migrations as queued jobs with logging and retries
  • Keep new columns nullable so old and new code can run side by side
  • Track which tenants are on which schema version
  • Automate backups and restores per tenant, not just for the server

When compliance drives the design

Regulated customers may require dedicated databases, audit trails, and region-specific hosting. Teams experienced in Laravel enterprise development usually offer a database-per-tenant tier for those accounts while keeping smaller tenants on shared infrastructure.

Lessons from production

Running multi-tenant systems at scale teaches the same lessons repeatedly.

  • Noisy neighbours are real: one large tenant can slow everyone, so add per-tenant rate limits and queue priorities.
  • Plan limits belong in code: tie features and usage caps to subscription tiers using tools such as Laravel Cashier and Pennant.
  • Support needs safe impersonation: log every time staff views a tenant’s account.
  • Tenant offboarding matters: build data export and deletion early.

A production example

In one Acquaint Softtech project, an EdTech learning platform moved from Laravel 6 to Laravel 11 with schema-based tenancy and Cashier billing across four tiers. It now serves 3,000+ active tenants with 99.97% uptime over 18 months and page loads 3.2 times faster.

Making the call

A good apartment building gets the locks, meters, and fire doors right before the first family moves in. Your SaaS needs the same discipline. Choose the tenancy model before launch, enforce isolation automatically at every layer, carry tenant context into jobs, cache and files, and plan migrations for scale. The rest of the product can grow safely on that foundation.

FAQs

Single or multi database for SaaS?

Start with a single database for most SaaS products. Use a database per tenant for enterprise or regulated customers who need strict isolation.

How do you isolate tenant data?

Scope every query by tenant automatically with global scopes, recheck ownership in policies, and test that cross-tenant access fails.

Which Laravel packages support multi-tenancy?

Tenancy for Laravel (stancl/tenancy) and Spatie’s laravel-multitenancy are the most widely used options.

Can you change tenancy models later?

Yes, but it means migrating every tenant’s data, so it is far cheaper to choose before launch.