Tenancy

Tenants and domains

Tenant model

Central table tenants:

  • id (string primary key; choose UUIDs or slugs in your app)
  • domain (nullable, unique; used in single-domain mode)
  • data (JSON bag for tenancy_db_name, tenancy_db_url, and your attributes)
  • timestamps
tenant := models.Tenant{ID: id}
tenant.SetDatabaseName("tenantacme")

Domains

Multi-domain mode uses the domains table (domain, tenant_id). Create with:

stored, err := pkg.CreateDomain(ctx, tenant.ID, "tenant.example.com")
// Use stored for redirects. With Laravel Herd active locally, stored will be
// "tenant.example.com.test" when Herd is available. See docs/herd.md.

Delete with pkg.DeleteDomain(ctx, host) (accepts the production or local Herd form).

Single-domain mode (Identification.SingleDomainTenants = true) stores the hostname on tenants.domain and skips the domains table for lookups. CreateTenant rewrites tenant.Domain in place when Herd aliases are active so redirects that read the struct see the real local hostname.

Hostnames listed in CentralDomains can never be assigned to tenants.

Local Herd aliases

On a Laravel Herd site with APP_ENV=local, the package will append .test and run herd link / herd unlink when Herd is available. Details: Herd.