Download the PHP package rylxes/laravel-multitenancy without Composer
On this page you can find all versions of the php package rylxes/laravel-multitenancy. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download rylxes/laravel-multitenancy
More information about rylxes/laravel-multitenancy
Files in rylxes/laravel-multitenancy
Package laravel-multitenancy
Short Description Simple by default, powerful when needed. Single-database and multi-database multitenancy for Laravel.
License MIT
Homepage https://github.com/rylxes/laravel-multitenancy
Informations about the package laravel-multitenancy
Laravel Multitenancy
Full Documentation — Complete usage guide, configuration reference, and API docs.
Simple, safe multi-tenancy for Laravel. Single-database by default, multi-database when you need it.
Why This Package?
| Pain Point | Solution |
|---|---|
| Data leakage between tenants | BelongsToTenant trait + safeguard mode throws on unscoped queries |
| Cache pollution | PrefixCacheTask auto-prefixes cache keys per tenant |
| Storage file mixing | PrefixFilesystemTask isolates disk paths per tenant |
| Queue jobs losing context | First-class integration with laravel-tenant-jobs |
| No fail-safe for missing scopes | Safeguard throws NoCurrentTenantException on unscoped queries |
| Migration complexity | tenant:migrate runs migrations across all tenants |
| Testing difficulty | ActingAsTenant trait for clean test setup |
| No CLI tenant management | 6 artisan commands out of the box |
Requirements
- PHP 8.1+
- Laravel 10, 11, or 12
Installation
Publish config and run migrations:
Quick Start (5 Minutes)
1. Create a tenant
2. Add tenant_id to your models' tables
3. Use the BelongsToTenant trait
That's it. All queries on Project are now automatically scoped to the current tenant, and tenant_id is auto-filled on creation.
4. Add middleware to your routes
Tenant Identification
The package resolves tenants from incoming requests. Choose a strategy in config/multitenancy.php:
| Finder | How it works | Best for |
|---|---|---|
DomainTenantFinder |
Matches request->getHost() |
Custom domains (acme.com) |
SubdomainTenantFinder |
Extracts first subdomain part | SaaS subdomains (acme.app.com) |
PathTenantFinder |
Uses first URL segment | Path-based (app.com/acme/...) |
HeaderTenantFinder |
Reads X-Tenant-ID header |
APIs and SPAs |
Data Isolation
BelongsToTenant Trait
Add to any Eloquent model to auto-scope all queries and auto-fill tenant_id:
Safeguard Mode
When safeguard is true (default), the package throws NoCurrentTenantException if you query a tenant-scoped model without an active tenant. This prevents accidental data leakage:
Admin/Landlord Queries
Bypass tenant scoping when needed:
Cache & Storage Isolation
Enabled by default via switch tasks:
Multi-Database Support
Uncomment SwitchDatabaseTask in config and set your tenant's database field:
Programmatic Usage
Artisan Commands
| Command | Description |
|---|---|
tenant:list |
List all tenants |
tenant:create {name} |
Create a tenant (--domain=, --database=) |
tenant:delete {id} |
Delete a tenant (with confirmation) |
tenant:migrate |
Run migrations for all tenants (--tenant= for one) |
tenant:seed |
Seed databases for all tenants |
tenant:artisan {command} |
Run any artisan command for all tenants |
Events
| Event | When |
|---|---|
TenantMadeCurrent |
After a tenant is set as current |
TenantForgotten |
After tenant context is cleared |
TenantCreated |
After a new tenant is created |
TenantDeleted |
After a tenant is deleted |
Queue Jobs
For tenant-aware queue jobs, install laravel-tenant-jobs:
Zero-config integration — laravel-tenant-jobs auto-detects this package and handles tenant context in all queued jobs, batch callbacks, retries, scheduled tasks, and notifications.
Testing
Use the ActingAsTenant trait in your tests:
Configuration Reference
Architecture
License
MIT. See LICENSE.
All versions of laravel-multitenancy with dependencies
illuminate/database Version ^10.0|^11.0|^12.0
illuminate/http Version ^10.0|^11.0|^12.0
illuminate/support Version ^10.0|^11.0|^12.0
illuminate/console Version ^10.0|^11.0|^12.0
illuminate/events Version ^10.0|^11.0|^12.0