Skip to main content

Claude/ChatGPT Prompt to Build Team-Based Multi-Tenancy in Laravel

Implement single-database, team-based multi-tenancy in Laravel: team_id scoping, global scopes, policies, an invitation flow and a switch-team endpoint.

Fill in the placeholders

Edit the values, then copy your finished prompt.

Your Prompt
prompt.txt

                                

What this prompt does

This prompt makes the AI act as a senior Laravel engineer and hand back a complete, drop-in multi-tenancy layer rather than a high-level explanation. You tell it your [laravel_version], your [tenancy_model], and your [auth_stack], and it produces the migrations, the global scope or trait, a TeamPolicy, middleware, an invitation flow, and a switch-team endpoint. The output is structured as six deliverables so nothing critical gets skipped.

The structure works because tenancy bugs are not random — they cluster in predictable places. By forcing the AI to name where tenancy leaks (queue jobs, un-scoped global queries, admin panels), the prompt converts a vague "make it multi-tenant" request into a concrete checklist. The [tenancy_model] variable is the most consequential: a single-database team_id approach produces very different code from a database-per-tenant model, so setting it precisely steers the entire answer. [auth_stack] tells the AI how to read the current team per request (Jetstream, Filament, plain Sanctum all differ).

When to use it

  • You're starting a new team-based SaaS and want the isolation layer correct from commit one.
  • You're retrofitting tenancy onto a single-tenant app and need a migration plan.
  • You keep worrying that a missing scope will let one team read another's rows.
  • You need an invitation + role flow and a switch-team endpoint scaffolded together.
  • You're using Filament or Nova and want the admin panel covered, not just the web app.
  • You want a written list of the exact spots where forgetting the scope leaks data.

Example output

You get a set of fenced code blocks: a migration set defining teams, memberships, and team_id foreign keys; a global scope or BelongsToTeam trait that auto-filters tenant-owned queries; a TeamPolicy plus middleware that blocks cross-team access even with a guessed ID; an invitation/accept/roles flow; and a switch-team endpoint. It closes with a usage example and an explicit callout of the three leak-prone areas and how to plug each.

Pro tips

  • Set [tenancy_model] exactly — "single database with a team_id column" yields very different code than database-per-tenant, and a vague value gives a vague answer.
  • Match [auth_stack] to your real stack so the "set current team per request" code fits; Jetstream, Filament, and bare Sanctum resolve the team differently.
  • Pin [laravel_version] to your actual version so the migration and scope syntax matches; mixing Laravel 10 and 12 idioms causes subtle breakage.
  • Lean hardest on deliverable 5 (the leak spots) before you ship — queue jobs that lose the current-team context are a classic source of cross-tenant bleaks.
  • Ask a follow-up to add tenant-aware tests; verifying that Team A genuinely cannot fetch Team B's rows is worth writing down.
  • If you use Filament, explicitly confirm the admin panel respects the scope, since admin queries often bypass it.

Frequently Asked Questions

Does this prompt cover single-database or database-per-tenant multi-tenancy?
It covers whichever model you put in the `[tenancy_model]` variable. The default is single-database with a shared `team_id` column, but you can set it to database-per-tenant and the generated migrations, scope, and isolation strategy will change to match that approach.
How does the generated code stop one team from reading another team's data?
It combines a global scope or trait that auto-filters every tenant-owned query by the current team with a `TeamPolicy` and middleware. Together these block access even when a user guesses another team's record ID, which is the most common leak.
Does it handle tenancy leaks in queue jobs and admin panels?
Yes, deliverable 5 specifically names the three places tenancy usually leaks — queue jobs, un-scoped global queries, and admin panels like Filament or Nova — and explains how to plug each. These are the spots where the global scope is most often bypassed.
Will it include the invitation and switch-team flow, or just the isolation layer?
Both. Beyond the scoping and policy code, it produces an invitation flow with invite, accept, and roles, plus a switch-team endpoint. You get the membership lifecycle alongside the data-isolation layer in one response.
Is the returned code production-ready as-is?
Treat it as a strong scaffold rather than a finished product. Review the leak-prone spots in deliverable 5 carefully and add tenant-aware tests before shipping, since the AI cannot see your full codebase or every place a query runs.
Engr Mejba Ahmed

Need this built for real?

Engr Mejba Ahmed

AI Developer · Software Engineer

I'm Mejba — I design and ship production AI systems, automations, and full-stack apps. If you want this turned into a working solution for your team, let's talk.

More in Laravel & PHP Prompts

Engr Mejba Ahmed

Engr Mejba Ahmed

Claude Code Expert · Online

👋

Hey there!

Quick Actions

WhatsApp Instant reply

Chat on WhatsApp

+880 1723 741224 · Instant reply

Popular Questions

Engr Mejba Ahmed is connected
Engr Mejba Ahmed is typing...
Engr Mejba Ahmed avatar

✉ Want me to follow up? Drop your email

Engr Mejba Ahmed avatar

📞 Connect Directly

Choose how you'd like to reach me

WhatsApp

+880 1723 741224

Email

[email protected]

✓ Details sent! I'll get back to you shortly.

Powered by OpenAI

335+

Blog Posts

25

AI Courses

63

Projects

Services & Expertise

Pricing & Process

Learning & Resources

Connect & Support