SaaS · Architecture · Guides
Laravel multi-tenancy explained: single database, schema or database per tenant.
What multi-tenancy means in Laravel, the three common designs, how tenant scoping works, the mistakes that leak data between customers, and how to choose.
Key takeaways
- Multi-tenancy means one application serving many customers (tenants) whose data must never mix.
- There are three common designs: a shared database with a tenant column, a schema per tenant, or a database per tenant.
- The cheapest design to start is also the easiest to get wrong, because one forgotten
whereclause leaks data. - Decide by how sensitive the data is, how many tenants you expect and how you will back up and migrate.
What a tenant is
A tenant is one customer organisation of your software: a hospital, a coaching institute, a travel agency. Its users should see only its data. A single-tenant app is deployed once per customer. A multi-tenant app runs once and serves them all, which makes updates and support much cheaper, but isolation becomes your job.
Design 1: shared database, tenant column
Every tenant-owned table gets a tenant_id. You add a global scope so every query is filtered automatically.
class TenantScope implements Scope
{
public function apply(Builder $builder, Model $model): void
{
if ($id = auth()->user()?->tenant_id) {
$builder->where($model->getTable() . '.tenant_id', $id);
}
}
}
Good: simple, cheap, one migration run, easy reporting across tenants. Risky: raw queries, jobs and console commands run without a logged-in user, so the scope silently does nothing. A single missed filter is a data leak.
Design 2: schema per tenant
Databases such as PostgreSQL support schemas, separate namespaces inside one database. Each tenant gets its own set of tables. Isolation is stronger than a column, and migrations still run from one place. MySQL treats schema and database as the same thing, so this design maps to design 3 there.
Design 3: database per tenant
Each tenant has its own database, and the app switches the connection when a request arrives, usually by subdomain or domain. Isolation is the strongest: a bad query cannot reach another tenant's rows, and you can back up, restore or delete one customer alone.
The costs are real. Every migration must run across all databases. Connection handling and queues need care. Cross-tenant reporting needs a separate step. Packages such as stancl/tenancy handle much of this, but you still need to understand what they do.
Mistakes that leak data
- Unscoped background jobs. Queue workers have no logged-in user. Pass the tenant id into the job and set the tenant before running it.
- Caching without a tenant key.
Cache::get('settings')can serve one tenant's settings to another. Prefix cache keys with the tenant. - File storage in one folder. Put uploads under a tenant-specific path.
- Trusting route IDs.
/invoices/15must still check that invoice 15 belongs to the current tenant. - Tests that only use one tenant. Write tests with two tenants and assert that tenant A cannot read tenant B.
How to choose
| Question | Leans toward |
|---|---|
| Hundreds of small customers, low sensitivity | Shared database |
| Few customers, regulated or sensitive data | Database per tenant |
| Customers may demand their own backup or export | Database per tenant |
| Heavy cross-tenant analytics | Shared database |
There is no universal answer. I explain one real trade-off in database per tenant vs shared database.
Frequently asked questions
Is multi-tenancy built into Laravel?
No. Laravel gives you global scopes, middleware and multiple database connections, which are the building blocks. Tenant resolution and switching are either written by you or provided by a package.
Can I start shared and move later?
Yes, but it is work. Adding a tenant_id from day one keeps the option open, even if you later split databases.
Does multi-tenancy affect performance?
A shared database grows into very large tables, so index tenant_id. Database per tenant keeps each database small but multiplies connections and migrations.
My hospital SaaS uses a separate database per hospital.
See the hospital SaaS