# spatie/laravel-multitenancy architecture

> Architecture diagram of the GitHub repository spatie/laravel-multitenancy, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-19). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/spatie/laravel-multitenancy
- Repository: https://github.com/spatie/laravel-multitenancy

## Overview

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: [`src/Tasks/TasksCollection.php`](https://github.com/spatie/laravel-multitenancy/blob/main/src/Tasks/TasksCollection.php) (Switch task collection), [`src/Multitenancy.php`](https://github.com/spatie/laravel-multitenancy/blob/main/src/Multitenancy.php) (Runtime coordinator), [`src/Actions/MakeTenantCurrentAction.php`](https://github.com/spatie/laravel-multitenancy/blob/main/src/Actions/MakeTenantCurrentAction.php) (Current tenant actions), [`src/Models/Tenant.php`](https://github.com/spatie/laravel-multitenancy/blob/main/src/Models/Tenant.php) (Tenant model)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_resolution["Tenant resolution"]
  node_tenant_finder["Tenant finder"]
  node_domain_finder["Domain finder"]
  node_tenant_model[("Tenant model<br/>[Tenant.php]")]
  node_tenant_collection["Tenant collection"]
end

subgraph group_lifecycle["Tenant lifecycle"]
  node_multitenancy["Runtime coordinator<br/>[Multitenancy.php]"]
  node_tenant_actions["Current tenant actions"]
  node_forget_action["Forget tenant action"]
  node_tenant_binding["Tenant binding"]
end

subgraph group_switching["Tenant switching"]
  node_tasks_collection["Switch task collection"]
  node_database_task["Database switch"]
  node_cache_task["Cache prefix switch"]
  node_route_task["Route cache switch"]
end

subgraph group_integrations["Runtime integrations"]
  node_middleware["Tenant middleware"]
  node_queue_worker(("Queue worker"))
  node_queue_action["Queue tenant integration"]
  node_tenant_jobs["Tenant-aware jobs<br/>[TenantAware.php]"]
  node_artisan_command["Per-tenant Artisan command"]
  node_model_connections["Model connections"]
end

node_request(("HTTP request"))
node_artisan_user(("Artisan operator"))

node_request -->|"starts resolution"| node_multitenancy
node_multitenancy -->|"resolves via"| node_tenant_finder
node_tenant_finder -.->|"can specialize as"| node_domain_finder
node_multitenancy -->|"makes current"| node_tenant_actions
node_tenant_model -->|"supports lifecycle"| node_tenant_actions
node_tenant_actions -->|"binds tenant"| node_tenant_binding
node_tenant_actions -->|"runs tasks"| node_tasks_collection
node_forget_action -->|"forgets tasks"| node_tasks_collection
node_multitenancy -->|"registers tasks"| node_tasks_collection
node_tasks_collection -.->|"may include"| node_database_task
node_tasks_collection -.->|"may include"| node_cache_task
node_tasks_collection -.->|"may include"| node_route_task
node_request -.->|"passes through"| node_middleware
node_multitenancy -->|"configures queue"| node_queue_action
node_queue_worker -->|"processes tenant jobs"| node_queue_action
node_queue_action -->|"recognizes interfaces"| node_tenant_jobs
node_artisan_user -->|"runs for tenants"| node_artisan_command
node_tenant_model -->|"provides collection"| node_tenant_collection
node_tenant_model -.->|"selects connection"| node_model_connections

click node_tenant_finder "https://github.com/spatie/laravel-multitenancy/tree/main/src/TenantFinder"
click node_domain_finder "https://github.com/spatie/laravel-multitenancy/blob/main/src/TenantFinder/DomainTenantFinder.php"
click node_tenant_model "https://github.com/spatie/laravel-multitenancy/blob/main/src/Models/Tenant.php"
click node_tenant_collection "https://github.com/spatie/laravel-multitenancy/blob/main/src/TenantCollection.php"
click node_multitenancy "https://github.com/spatie/laravel-multitenancy/blob/main/src/Multitenancy.php"
click node_tenant_actions "https://github.com/spatie/laravel-multitenancy/blob/main/src/Actions/MakeTenantCurrentAction.php"
click node_forget_action "https://github.com/spatie/laravel-multitenancy/blob/main/src/Actions/ForgetCurrentTenantAction.php"
click node_tenant_binding "https://github.com/spatie/laravel-multitenancy/blob/main/src/Concerns/BindAsCurrentTenant.php"
click node_tasks_collection "https://github.com/spatie/laravel-multitenancy/blob/main/src/Tasks/TasksCollection.php"
click node_database_task "https://github.com/spatie/laravel-multitenancy/blob/main/src/Tasks/SwitchTenantDatabaseTask.php"
click node_cache_task "https://github.com/spatie/laravel-multitenancy/blob/main/src/Tasks/PrefixCacheTask.php"
click node_route_task "https://github.com/spatie/laravel-multitenancy/blob/main/src/Tasks/SwitchRouteCacheTask.php"
click node_middleware "https://github.com/spatie/laravel-multitenancy/tree/main/src/Http/Middleware"
click node_queue_action "https://github.com/spatie/laravel-multitenancy/blob/main/src/Actions/MakeQueueTenantAwareAction.php"
click node_tenant_jobs "https://github.com/spatie/laravel-multitenancy/blob/main/src/Jobs/TenantAware.php"
click node_artisan_command "https://github.com/spatie/laravel-multitenancy/blob/main/src/Commands/TenantsArtisanCommand.php"
click node_model_connections "https://github.com/spatie/laravel-multitenancy/blob/main/src/Models/Concerns/UsesTenantConnection.php"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_tenant_finder,node_domain_finder,node_tenant_model,node_tenant_collection toneBlue
class node_multitenancy,node_tenant_actions,node_forget_action,node_tenant_binding toneAmber
class node_tasks_collection,node_database_task,node_cache_task,node_route_task,node_request toneMint
class node_middleware,node_queue_worker,node_queue_action,node_tenant_jobs,node_artisan_command,node_model_connections toneRose
class node_artisan_user toneIndigo
```

## Components

### Tenant resolution

- [Tenant finder](https://github.com/spatie/laravel-multitenancy/tree/main/src/TenantFinder): `src/TenantFinder`
- [Domain finder](https://github.com/spatie/laravel-multitenancy/blob/main/src/TenantFinder/DomainTenantFinder.php): `src/TenantFinder/DomainTenantFinder.php`
- [Tenant model](https://github.com/spatie/laravel-multitenancy/blob/main/src/Models/Tenant.php): `src/Models/Tenant.php`
- [Tenant collection](https://github.com/spatie/laravel-multitenancy/blob/main/src/TenantCollection.php): `src/TenantCollection.php`

### Tenant lifecycle

- [Runtime coordinator](https://github.com/spatie/laravel-multitenancy/blob/main/src/Multitenancy.php): `src/Multitenancy.php`
- [Current tenant actions](https://github.com/spatie/laravel-multitenancy/blob/main/src/Actions/MakeTenantCurrentAction.php): `src/Actions/MakeTenantCurrentAction.php`
- [Forget tenant action](https://github.com/spatie/laravel-multitenancy/blob/main/src/Actions/ForgetCurrentTenantAction.php): `src/Actions/ForgetCurrentTenantAction.php`
- [Tenant binding](https://github.com/spatie/laravel-multitenancy/blob/main/src/Concerns/BindAsCurrentTenant.php): `src/Concerns/BindAsCurrentTenant.php`

### Tenant switching

- [Switch task collection](https://github.com/spatie/laravel-multitenancy/blob/main/src/Tasks/TasksCollection.php): `src/Tasks/TasksCollection.php`
- [Database switch](https://github.com/spatie/laravel-multitenancy/blob/main/src/Tasks/SwitchTenantDatabaseTask.php): `src/Tasks/SwitchTenantDatabaseTask.php`
- [Cache prefix switch](https://github.com/spatie/laravel-multitenancy/blob/main/src/Tasks/PrefixCacheTask.php): `src/Tasks/PrefixCacheTask.php`
- [Route cache switch](https://github.com/spatie/laravel-multitenancy/blob/main/src/Tasks/SwitchRouteCacheTask.php): `src/Tasks/SwitchRouteCacheTask.php`

### Runtime integrations

- [Tenant middleware](https://github.com/spatie/laravel-multitenancy/tree/main/src/Http/Middleware): `src/Http/Middleware`
- **Queue worker**
- [Queue tenant integration](https://github.com/spatie/laravel-multitenancy/blob/main/src/Actions/MakeQueueTenantAwareAction.php): `src/Actions/MakeQueueTenantAwareAction.php`
- [Tenant-aware jobs](https://github.com/spatie/laravel-multitenancy/blob/main/src/Jobs/TenantAware.php): `src/Jobs/TenantAware.php`
- [Per-tenant Artisan command](https://github.com/spatie/laravel-multitenancy/blob/main/src/Commands/TenantsArtisanCommand.php): `src/Commands/TenantsArtisanCommand.php`
- [Model connections](https://github.com/spatie/laravel-multitenancy/blob/main/src/Models/Concerns/UsesTenantConnection.php): `src/Models/Concerns/UsesTenantConnection.php`

### Other components

- **HTTP request**
- **Artisan operator**

## Connections

- HTTP request → Runtime coordinator: starts resolution (evidence: `README.md`)
- Runtime coordinator → Tenant finder: resolves via (evidence: `src/Multitenancy.php`)
- Tenant finder → Domain finder: can specialize as (evidence: `src/TenantFinder/DomainTenantFinder.php`)
- Runtime coordinator → Current tenant actions: makes current (evidence: `src/Models/Concerns/ImplementsTenant.php`)
- Tenant model → Current tenant actions: supports lifecycle
- Current tenant actions → Tenant binding: binds tenant (evidence: `src/Actions/MakeTenantCurrentAction.php`)
- Current tenant actions → Switch task collection: runs tasks (evidence: `src/Actions/MakeTenantCurrentAction.php`)
- Forget tenant action → Switch task collection: forgets tasks (evidence: `src/Actions/ForgetCurrentTenantAction.php`)
- Runtime coordinator → Switch task collection: registers tasks (evidence: `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 (evidence: `src/Multitenancy.php`)
- Queue worker → Queue tenant integration: processes tenant jobs (evidence: `README.md`)
- Queue tenant integration → Tenant-aware jobs: recognizes interfaces (evidence: `src/Actions/MakeQueueTenantAwareAction.php`)
- Artisan operator → Per-tenant Artisan command: runs for tenants (evidence: `README.md`)
- Tenant model → Tenant collection: provides collection
- Tenant model → Model connections: selects connection (evidence: `README.md`)
