Overview
SaaS Starter Vue implements a database-per-tenant architecture using Stancl/Tenancy. Each tenant gets:- A dedicated database for complete data isolation
- A unique subdomain (e.g.,
acme.yourdomain.com) - Automatic database creation and migration
- Independent user authentication
The multi-tenancy system uses domain-based identification. Tenants are automatically identified by their subdomain when accessing the application.
How It Works
Central vs Tenant Domains
The application operates on two domain types: Central Domain (app.yourdomain.com)
- Admin dashboard and management
- Tenant creation and billing
- System-wide settings
- Plan management
{subdomain}.yourdomain.com)
- Individual tenant workspaces
- Tenant-specific users and data
- Isolated authentication
Database Architecture
Each tenant gets a separate PostgreSQL database:- Creates the database when a tenant is provisioned
- Runs migrations to set up the schema
- Seeds initial data (admin user)
- Configures domain routing
Creating Tenants
From Admin Dashboard
Tenants can be created by system administrators:1
Navigate to Tenants
Access the Tenants management page from your admin dashboard at
/tenants2
Fill Tenant Information
Provide the following details:
- Company name
- Subdomain (unique identifier)
- Owner name and email
- Owner password
- Subscription plan
3
Automatic Provisioning
The system automatically:
- Creates a dedicated database
- Sets up the domain routing
- Creates the admin user
- Initializes the tenant workspace
Self-Service Registration
If guest registration is enabled, users can sign up directly:View Guest Registration Code
View Guest Registration Code
Tenant Isolation
Middleware Protection
Tenant routes are protected by specialized middleware:Database Context Switching
TheInitializeTenancyByDomain middleware automatically:
- Identifies the tenant from the subdomain
- Switches the database connection to the tenant’s database
- Makes tenant data available throughout the request
Tenant Lifecycle
Active Status
Trial- In trial periodActive- Active subscriptionCanceled- Canceled with grace periodExpired- Trial or subscription expired
Cancellation and Grace Period
When a tenant is canceled, they enter a 30-day grace period:1
Cancellation
Tenant is marked as canceled and
canceled_at timestamp is set2
Grace Period
Tenant has 30 days to restore their account
3
Permanent Deletion
After 30 days, the tenant database is permanently deleted
Restoration
Canceled tenants can be restored within the grace period:Tenant Model
The Tenant model extends Stancl’s base tenant:Relationships
belongsTo
The subscription plan assigned to this tenant
hasMany
All subscription records for this tenant
hasOne
The most recent subscription record
hasMany
Domain records associated with this tenant (inherited from
HasDomains)Working with Tenant Context
Running Code in Tenant Context
When you need to execute code within a tenant’s database context:run() method ensures all queries execute against the tenant’s database.
Domain Configuration
Subdomain Format
Tenant domains are created with this pattern:Environment Configuration
Configure your base domain in.env:
Make sure your DNS wildcard record points
*.yourdomain.com to your application server for subdomain routing to work correctly.Best Practices
- Always use middleware - Let the framework handle tenant identification
- Test isolation - Ensure tenant data never leaks between databases
- Monitor database growth - Each tenant database grows independently
- Backup strategy - Implement per-tenant backup solutions
- Migration testing - Test tenant migrations thoroughly before deployment
API Endpoints
Tenant Management
auth, verified middleware).