Powerful multi-tenancy for Laravel Go
Identify a tenant from the request, keep state on
context.Context, and isolate data by default with a
dedicated database per tenant. Built for concurrent Go servers;
no process-global “current tenant.”
go get github.com/martinshaw/laravel-go-tenancy@latest Subdomain, Domain, Path, Origin …
Tenant context
Database, Cache, Assets …
Batteries for tenancy. Idiomatic Go. Identification, bootstrappers, and database isolation designed to work as one package on top of Laravel Go.
Requests
- Identification Domain, subdomain, path, header, query, cookie, or Origin.
- Early identification Central, tenant, and universal route modes.
- Laravel Herd Local tenant host aliases that track your domains.
Runtime
- Context state Tenant lives on
context.Contextonly. - Bootstrappers Database, cache, filesystem, queue, and session.
- Commands Migrate, seed, and run artisan-style work per tenant.
Data
- Multi-database Pooled per-tenant connections by default.
- Single-database Opt-in shared DB with hard tenant scoping.
- Security Fail closed. Central only via
tenancy.Central.
Identify from the request.
Middleware resolvers map host, path, or request data to a tenant. Unknown identity denies by default: no silent fall-through to the central connection.
Identification docspkg.OnCentral(route, func(route *routing.Router) {
route.Get("/", centralHome)
})
route.Middleware(pkg.PreventCentral(), pkg.ByDomain()).Group(func(route *routing.Router) {
route.Get("/", tenantHome)
route.Get("/dashboard", dashboard)
})
Bootstrap the tenant scope.
When a tenant initializes, bootstrappers swap database, cache, filesystem, queue, and session into the request context. End tears them down cleanly.
Bootstrappers docsctx, err := tenancy.Initialize(ctx, tenant)
if err != nil {
return web.Fail(err)
}
defer tenancy.End(ctx)
db := database.DB(ctx) // tenant connection
Isolate by default.
Multi-database tenancy pools per-tenant *db.DB handles.
Single-database mode is explicit. Models that belong to a tenant
error when no tenant is in context.
// BelongsToTenant errors without a tenant
posts, err := model.Query[Post]().
Get(ctx)
Security is a contract.
Isolation guarantees are tested. Central database access is only
through tenancy.Central(ctx) after the database
bootstrapper. Identification failures return 404, not the wrong data.
- No package-level current tenant
- Fail closed on unknown identity
- Pooled connections closed on delete
security_test.goguards the invariants
Start with the docs.
This site is generated from the package docs/ markdown
on every release. What you read here is what ships in the repository.