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.”

Get started
go get github.com/martinshaw/laravel-go-tenancy@latest
identify → initialize → isolate pools warm fail-closed

Batteries for tenancy. Idiomatic Go. Identification, bootstrappers, and database isolation designed to work as one package on top of Laravel Go.

Requests

Runtime

  • Context state Tenant lives on context.Context only.
  • Bootstrappers Database, cache, filesystem, queue, and session.
  • Commands Migrate, seed, and run artisan-style work per tenant.

Data

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 docs
pkg.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 docs
ctx, 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.

Multi-database docs
// 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.

Security docs
  • No package-level current tenant
  • Fail closed on unknown identity
  • Pooled connections closed on delete
  • security_test.go guards 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.