How spatie/laravel-multitenancy works
This is Spatie’s Laravel multitenancy package: it resolves a tenant for a request, makes that tenant current, and runs configured switching tasks. The README also describes tenant-aware queued jobs, Artisan commands run per tenant, and model connection selection. The graph preserves those product capabilities and the cache, route-cache, and database switching tasks present in the tree. Source excerpts show the central tenant lifecycle and database task; links for unsampled capabilities rely on README statements or are omitted where wiring is not established.
Main entry points: TasksCollection.php Switch task collection · Multitenancy.php Runtime coordinator · MakeTenantCurrentAction.php Current tenant actions · Tenant.php Tenant model
Components 20
Tenant resolution
- Tenant finder
src/TenantFinder - Domain finder
src/TenantFinder/DomainTenantFinder.php - Tenant model
src/Models/Tenant.php - Tenant collection
src/TenantCollection.php
Tenant lifecycle
- Runtime coordinator
src/Multitenancy.php - Current tenant actions
src/Actions/MakeTenantCurrentAction.php - Forget tenant action
src/Actions/ForgetCurrentTenantAction.php - Tenant binding
src/Concerns/BindAsCurrentTenant.php
Tenant switching
- Switch task collection
src/Tasks/TasksCollection.php - Database switch
src/Tasks/SwitchTenantDatabaseTask.php - Cache prefix switch
src/Tasks/PrefixCacheTask.php - Route cache switch
src/Tasks/SwitchRouteCacheTask.php
Runtime integrations
- Tenant middleware
src/Http/Middleware - Queue worker
- Queue tenant integration
src/Actions/MakeQueueTenantAwareAction.php - Tenant-aware jobs
src/Jobs/TenantAware.php - Per-tenant Artisan command
src/Commands/TenantsArtisanCommand.php - Model connections
src/Models/Concerns/UsesTenantConnection.php
Other components
- HTTP request
- Artisan operator
Connections 19
- HTTP request → Runtime coordinator: starts resolution
README.md - Runtime coordinator → Tenant finder: resolves via
src/Multitenancy.php - Tenant finder → Domain finder: can specialize as
src/TenantFinder/DomainTenantFinder.php - Runtime coordinator → Current tenant actions: makes current
src/Models/Concerns/ImplementsTenant.php - Tenant model → Current tenant actions: supports lifecycle
- Current tenant actions → Tenant binding: binds tenant
src/Actions/MakeTenantCurrentAction.php - Current tenant actions → Switch task collection: runs tasks
src/Actions/MakeTenantCurrentAction.php - Forget tenant action → Switch task collection: forgets tasks
src/Actions/ForgetCurrentTenantAction.php - Runtime coordinator → Switch task collection: registers tasks
src/Multitenancy.php - Switch task collection → Database switch: may include
- Switch task collection → Cache prefix switch: may include
- Switch task collection → Route cache switch: may include
- HTTP request → Tenant middleware: passes through
- Runtime coordinator → Queue tenant integration: configures queue
src/Multitenancy.php - Queue worker → Queue tenant integration: processes tenant jobs
README.md - Queue tenant integration → Tenant-aware jobs: recognizes interfaces
src/Actions/MakeQueueTenantAwareAction.php - Artisan operator → Per-tenant Artisan command: runs for tenants
README.md - Tenant model → Tenant collection: provides collection
- Tenant model → Model connections: selects connection
README.md