·2 min read
Multi-tenant SaaS in Laravel: choosing a tenancy model
One database or many? A practical comparison of the three common approaches, and the simple one I reach for first.
Laravel / SaaS / Architecture

Every SaaS answers the same question early: when two companies sign up, how do you keep their data apart? The choice affects your queries, your migrations, your backups and your hosting bill, so it’s worth getting right.
The three options
1. One database, a tenant_id column on every table.
All customers share the same tables, and each row records which tenant owns it. Cheapest to run and simplest to deploy. The risk: forget one where tenant_id = ? and one customer sees another’s data.
2. One database, a schema per tenant. Each tenant gets its own set of tables inside the same database server. Stronger separation, but migrations now run once per tenant, and some hosts and tools handle many schemas poorly.
3. A database per tenant. The strongest isolation, and easy to back up, restore or delete one customer. It’s also the most to operate: connection switching, many migrations, more infrastructure. Usually worth it only when contracts or regulations demand it.
Where I start
For most new products, option 1. It’s the fastest to build, and the main risk can be removed with code rather than discipline.
In Laravel, a global scope applies the tenant filter to every query automatically:
// app/Models/Concerns/BelongsToTenant.php
trait BelongsToTenant
{
protected static function bootBelongsToTenant(): void
{
static::addGlobalScope('tenant', function (Builder $query) {
if ($tenantId = app('currentTenant')?->id) {
$query->where($query->getModel()->getTable() . '.tenant_id', $tenantId);
}
});
static::creating(function (Model $model) {
$model->tenant_id ??= app('currentTenant')?->id;
});
}
}
Add the trait to every tenant-owned model, and set currentTenant once per request in middleware, from the subdomain or the logged-in user’s workspace. From then on, Booking::all() only ever returns the current tenant’s bookings.
The details that bite later
- Unique rules need the tenant. An invoice number should be unique per tenant, not globally. Same for emails if one person can belong to several workspaces.
- Queued jobs lose the request. A job running in the background doesn’t know which tenant it belongs to. Store the tenant ID on the job and restore it when the job runs.
- File storage needs a prefix. Put uploads under
tenants/{id}/...so a bad path can’t serve another customer’s file. - Write a test that tries to leak. Create two tenants, log in as one, and assert that every list and detail page returns nothing from the other. Keep it in the test suite forever.
Leave a door open
Keep tenant resolution in one place. If a large customer later needs their own database, you can switch just that tenant’s connection without rewriting the app. Start simple, and make the upgrade path cheap.