Digging Deeper

Example applications

Runnable demos that show how to wire laravel-go-tenancy into a Laravel for Go app. Each example lives under examples/ as its own Go module so you can add more demos without changing the published module path.

These examples target Laravel Herd: Herd builds and starts the app on demand when you open the .test site. See also Laravel Herd local aliases for how tenant hostnames become site aliases, and Identification for middleware choices.

CI runs go test ./... and go build in every examples/*/ module (see .github/workflows/ci.yml).

Prerequisites

  • Laravel Herd with the Go runtime installed (Herd → Settings → Runtimes → Go)
  • Go 1.27+ on your PATH (Herd can install this)
  • This repository cloned locally

Catalogue

Example Identification Herd site (central)
saas-helpdesk Product demo (OnCentral + BySubdomain) laravel-go-tenancy-example-saas-helpdesk.com.test
basic ByDomain laravel-go-tenancy-example-basic.com.test
path ByPath (/{tenant}/) laravel-go-tenancy-example-path.com.test
subdomain BySubdomain laravel-go-tenancy-example-subdomain.com.test
domain-or-subdomain ByDomainOrSubdomain laravel-go-tenancy-example-domain-or-subdomain.com.test
request-data ByRequestData laravel-go-tenancy-example-request-data.com.test
origin ByOriginHeader laravel-go-tenancy-example-origin.com.test

Each module has ./scripts/setup.sh, reset-data.sh, teardown.sh, a README.md, and go test ./... coverage for its routes.

Shared workflow

cd examples/<name>
./scripts/setup.sh
# open the central .test URL (and tenant URLs from that example’s README)
go test ./...
./scripts/reset-data.sh   # wipe SQLite; stop Herd app servers; reseed on next hit
./scripts/teardown.sh     # unlink Herd sites + aliases, remove storage/

setup.sh runs go mod tidy, herd link for the example apex, checks herd which, and stops stale Herd Go processes. Prefer letting Herd start the app via the .test URL so SERVER_PORT comes from Herd’s proxy.

Site names and tenant aliases are defined in each example’s scripts/site.sh (keep in sync with app.go / .env).

Herd process note

Herd starts a separate Go process per linked hostname (central + each tenant alias); binaries often live in the go-build cache, so a naive pkill on the project path can miss them. Example scripts/site.sh stops those upstreams by listen port (and any process still holding storage/ FDs). After code or data changes, use ./scripts/reset-data.sh (or teardown.sh) so every alias drops stale SQLite handles — otherwise one host can keep reading a deleted DB file while another writes a newly created one at the same path.

SaaS helpdesk (examples/saas-helpdesk)

Practical multi-tenant support desk: central host for marketing, org list, signup, and a cross-org ticket list that closes rows via pkg.Run into each tenant DB; each organization is a subdomain with its own ticket inbox and SQLite database. A DB debug dialog on every page dumps the active connection’s tables. Shows isolation (Acme tickets never appear on Beta) rather than another identification-method sample. Uses OnCentral + PreventCentral / BySubdomain.

Basic (examples/basic): domain

Multi-database app with full hostname identification and automatic Herd aliases for tenant hosts.

Seeds domains acme. / beta. + apex; when Herd aliases are active they are rewritten to .test and linked.

Path (examples/path)

Path parameter identification (pkg.ByPath() with Prefix("{tenant}")). One Herd site; tenants differ by path only.

Subdomain (examples/subdomain)

Subdomain of the central apex (pkg.BySubdomain()). Tenant domains column stores the subdomain label (acme); Herd aliases are acme.{apex}.

Domain or subdomain (examples/domain-or-subdomain)

pkg.ByDomainOrSubdomain(): subdomain of the apex when applicable, otherwise full host. Demo seeds acme as a subdomain tenant and beta.custom.test as a full-domain tenant.

Request data (examples/request-data)

pkg.ByRequestData(): header X-Tenant, then query tenant, then cookie tenant (same host for all tenants).

curl -H 'X-Tenant: acme' http://laravel-go-tenancy-example-request-data.com.test/me

Origin (examples/origin)

pkg.ByOriginHeader(): tenant from the host of the Origin header (CORS-style clients).

curl -H 'Origin: https://acme.laravel-go-tenancy-example-origin.com.test' \
  http://laravel-go-tenancy-example-origin.com.test/cors