Download the PHP package mahdisphp/laravel-realm without Composer
On this page you can find all versions of the php package mahdisphp/laravel-realm. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package laravel-realm
What is Realm?
Realm adds multi-tenancy to your Laravel app with a single trait and a safety-first approach. It's designed for the 80% of SaaS apps that need row-level tenant isolation in a single database.
Column strategy only. Database-per-tenant and schema-per-tenant strategies are coming in v1.0.
What makes Realm different?
- Fail-closed by default. If no tenant context is active, queries on tenant models return empty results — not all tenants' data. Data leaks should be impossible by default.
- Clean naming. The
Realmfacade andTenantmodel are separate classes with zero method overlap. No ambiguity. - One trait, one macro. Add
BelongsToRealmto a model and$table->realm()to a migration. That's it. - Octane-ready. Context is automatically reset between requests. No manual setup required.
What Realm is NOT (yet)
- Battle-tested. This is a new package. Use in production at your own risk and report bugs.
- A replacement for stancl/tenancy. Stancl has years of production edge cases discovered and fixed. If you need database-per-tenant today, use stancl.
Requirements
- PHP 8.2+
- Laravel 11, 12, or 13
Installation
Quick Start
1. Add the trait to your model
2. Add the column to your migration
3. Wrap your routes
4. That's it
Key Concepts
The Facade vs The Model
Strict Mode (Default: ON)
When no tenant context is active:
- Model queries return empty results (
WHERE 0 = 1) and log a warning - Model creates throw
NoActiveRealmException - Cache and storage fall back to global (unprefixed) space
Escape Hatches
Shared vs Tenant Models
Not every model needs tenant scoping:
Cache / Queue / Storage Isolation
Cache Key Prefixing
When a tenant is active, all cache keys are automatically prefixed with realm:{tenant_key}:. No code changes needed.
Queue Context Propagation
Add the RealmAwareJob trait to your jobs. It captures the tenant context at dispatch time and restores it when the job is processed.
- In strict mode, dispatching without context throws
NoActiveRealmException - If the tenant is deleted between dispatch and processing, the job is failed
- If the tenant is deactivated, the job is released with a configurable delay
Storage Path Prefixing
All storage paths are automatically prefixed with tenants/{tenant_key}/:
Encrypted Secrets
Store sensitive per-tenant configuration (API keys, tokens) encrypted at rest:
Secrets are:
- Encrypted via Laravel's
Cryptfacade - Hidden from
toArray()/toJson()(never accidentally serialized) - Cascade-deleted when the tenant is deleted
Tenant User Relationships
The tenant_users pivot table links users to tenants with roles:
Per-Tenant Config Overrides
Store non-sensitive config overrides in the tenant's data JSON column:
API keys and secrets should use
setSecret(), not config overrides.
Resolvers
Realm resolves the tenant from the incoming request using a pipeline of resolvers. The first match wins.
| Resolver | Config | Example |
|---|---|---|
SubdomainResolver |
realm.subdomain.domain |
acme.myapp.com |
HeaderResolver |
realm.header.name |
X-Realm: acme |
PathResolver |
realm.path.segment |
myapp.com/acme/dashboard |
DomainResolver |
— | app.acmecorp.com (full custom domain) |
QueryResolver |
realm.query.parameter |
?realm=acme |
SessionResolver |
realm.session.key |
Session-based switching |
AuthResolver |
— | Resolves from authenticated user's tenant |
Configure active resolvers in config/realm.php:
Commands
Testing
Blueprint Macro
restrictOnDelete is the default — deleting a tenant fails with a foreign key error, forcing you to handle data cleanup explicitly.
Configuration
After installation, the config lives at config/realm.php:
strategy—'column'(only option currently)resolvers— array of resolver classesstrict—true(fail-closed on missing context)cache.prefix—true(auto-prefix cache keys per tenant)queue.context—true(propagate tenant context to queued jobs)storage.prefix_path—true(auto-prefix storage paths per tenant)user_model— the user model class for tenant-user relationshipson_fail—'abort'(abort/redirect/exception on resolution failure)
Why not stancl/tenancy?
Use stancl if you need database-per-tenant today — it has years of battle-testing we don't. Realm is for the 80% of SaaS apps that only need column-based row isolation and want it with one trait, one macro, and fail-closed defaults out of the box.
Roadmap
- v0.1 — Column strategy, subdomain + header resolution, strict mode, test helpers
- v0.2 (current) — Cache/queue/storage isolation, encrypted secrets, tenant users, 5 new resolvers, per-tenant config overrides,
realm:delete/realm:exec/realm:run-for-eachcommands - v1.0 — Database-per-tenant and schema-per-tenant strategies
License
MIT. See LICENSE.
All versions of laravel-realm with dependencies
illuminate/contracts Version ^11.0|^12.0|^13.0
illuminate/database Version ^11.0|^12.0|^13.0
illuminate/http Version ^11.0|^12.0|^13.0
illuminate/support Version ^11.0|^12.0|^13.0
illuminate/console Version ^11.0|^12.0|^13.0
illuminate/log Version ^11.0|^12.0|^13.0