How Multi-Tenancy Works
OptiFlow implements database-per-tenant architecture, where each tenant has its own isolated database:- Central Database: Stores tenant metadata, client information, and manages tenant provisioning
- Tenant Databases: Each tenant gets a separate database (named
optiflow_{tenant_id}) containing their business data - Domain-Based Access: Tenants are identified by subdomain (e.g.,
clientname.optiflow.com.do)
Data Separation
The multi-tenancy package ensures complete data isolation:- Database connections are automatically switched based on the current tenant
- Cache is scoped per tenant using tags
- File storage is separated using tenant-specific paths
- Queue jobs maintain tenant context
The central database uses the
central connection, while tenant databases use the tenant connection that is dynamically created at runtime.Central vs Tenant Databases
Central Database Tables
tenants- Tenant metadata and configurationdomains- Subdomain mappings for each tenantclients- Client/customer information
Tenant Database Tables
Each tenant database contains:- Users and workspace data
- Invoices, quotations, prescriptions
- Products, clients, contacts
- Company details and configurations
- Document subtypes (NCF sequences)
- Currencies and rates
Subdomain-Based Tenant Access
Tenants are accessed via subdomains configured in theconfig/tenancy.php file:
acme.optiflow.com.do, the tenancy middleware:
- Identifies the tenant from the subdomain
- Initializes tenant context
- Switches to the tenant’s database
- Loads tenant-specific configurations
Creating and Managing Tenants
Creating a New Tenant
Tenants are created through the Filament admin panel atapp/Filament/Resources/Tenants/:
Create Tenant Record
Navigate to the Tenants resource in Filament admin and create a new tenant with:
- Unique tenant ID
- Tenant name
- Subdomain/domain
- Associated client
Database Creation
The tenant database is automatically created when the tenant record is saved. The database name follows the pattern:
optiflow_{tenant_id}Run Migrations
Tenant-specific migrations from
database/migrations/tenant/ are automatically run using:Tenant Model Reference
TheApp\Models\Central\Tenant model (located at app/Models/Central/Tenant.php:42) implements the tenant functionality:
Tenancy Configuration
Key configuration options inconfig/tenancy.php:
Database Settings
Bootstrappers
These make Laravel features tenant-aware:DatabaseTenancyBootstrapper- Switches database connectionsCacheTenancyBootstrapper- Scopes cache by tenantFilesystemTenancyBootstrapper- Separates file storageQueueTenancyBootstrapper- Maintains tenant context in queued jobs
Working with Tenant Context
Running Commands for Specific Tenants
Accessing Tenant Data in Code
Best Practices
- Never Mix Contexts: Don’t query central models from tenant routes or vice versa
- Use Middleware: Always apply tenancy middleware to tenant routes
- Test Isolation: Verify data isolation between tenants in testing
- Backup Strategy: Implement per-tenant backup strategies
- Migrations: Keep tenant migrations in
database/migrations/tenant/directory
Related Resources
- Workspace Management - Managing workspaces within tenants
- User Roles & Permissions - Configuring user access
- Official Tenancy Documentation