Skip to content
← All articles

Single-database multi-tenancy without the foot-guns

· TenantForge team

Most SaaS products start with a handful of customers and grow to thousands. A single database with a tenant_id column keeps operations simple: one schema, one migration run, one backup, and reports that span every customer.

The risk is obvious: forget one where clause and a customer sees another customer's data. TenantForge removes that risk with three layers.

1. A global scope that fails closed

Every tenant-owned model uses the BelongsToTenant trait. Its scope filters by the current workspace, and when no workspace is set the query returns nothing instead of everything. Code that genuinely needs all tenants must say so explicitly with withoutTenancy().

2. Tenancy before route model binding

The middleware that resolves the workspace runs before Laravel binds {project} from the URL. A project id from another workspace simply returns a 404.

3. Guards on write

Creating a record fills tenant_id automatically, and saving a record that belongs to a different workspace throws an exception. Policies double-check the tenant on top.

Proving it

The test suite includes isolation tests for queries, route model binding, the search endpoint and the REST API. Run them after every change:

php artisan test --filter=TenantIsolation