If you're building a SaaS in Laravel, one of the first big choices is how to keep each customer's data apart. There are two common ways:
- Single database: every table has a
tenant_idcolumn, and every query must filter by it. - Database per tenant: every customer gets their own database.
This post is about the second one, and how the "central" and "tenant" sides fit together.
Two sides of the same app
A database-per-tenant app really has two parts:
The central app runs on your main domain, like your-app.com. This is where you, the owner, work. The central database holds:
- the list of tenants
- their domains
- your own admin users
- global settings
- the queue
Tenant workspaces run on subdomains, like acme.your-app.com. Each tenant database holds that customer's own data: users, roles, menus, sessions, and whatever your product adds.
The domain of each request decides which tenant is active. Visit acme.your-app.com, and Laravel switches to Acme's database for that request.
Setting it up with stancl/tenancy
The most common package for this is stancl/tenancy. In multi-database mode it:
- finds the tenant from the domain,
- switches the database connection,
- runs the request against that tenant's database.
Your migrations split into two folders:
database/migrations -> central tables database/migrations/tenant -> tables every tenant gets When you create a tenant, its database is created and the tenant migrations run on their own. For existing tenants:
php artisan tenants:migrate
php artisan tenants:migrate --tenants=1
php artisan tenants:seed
Things people forget
The database is only one part. A few other places need to know about the tenant too:
- Cache: tag entries per tenant, so one tenant never reads another's cache.
- File storage: give each tenant its own folder.
- Queued jobs: make sure a job remembers which tenant it belongs to, or it will run against the wrong database.
- Database permissions: your DB user must be allowed to
CREATE DATABASE, or tenant creation fails. - Local setup: you need wildcard subdomains. Laravel Herd serves
*.testwith no extra setup.
When it's worth it
Database per tenant is a good fit when customers expect their data to be truly separate, or when you might need to back up, move or delete one customer on its own. The trade-off is more moving parts: more databases to migrate and more to watch.
If most of your customers are small and you want the simplest setup, a single database with tenant_id can be enough.
If you'd rather not build this part yourself
I built SaaS Laravel, a Laravel 13 starter kit that has all of this already working: central admin, tenant workspaces on subdomains, auth with 2FA and passkeys, roles and permissions, and 17 languages. It comes in Vue, React, Svelte and Livewire, with a one-time payment.
Either way, I hope the central vs tenant split is clearer now. Questions are welcome in the comments.