Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laravel Multitenancy Laravel Package

spatie/laravel-multitenancy

Unopinionated multitenancy for Laravel. Detect the current tenant per request and run configurable tasks when switching tenants. Supports single or multiple databases, tenant-aware queued jobs, per-tenant Artisan commands, and easy model connection handling.

View on GitHub
Deep Wiki
Context7

Product Decisions This Supports

  • Multi-Tenant SaaS Platform: Enables rapid development of tenant-isolated applications (e.g., customer portals, white-label solutions) by abstracting database/connection management.
  • Database-Per-Tenant vs. Shared Schema: Supports both architectures (single/multi-database) with minimal refactoring, reducing technical debt.
  • Build vs. Buy: Avoids reinventing multitenancy logic (e.g., tenant resolution, job isolation) while maintaining flexibility for customization.
  • Roadmap Priorities:
    • Phase 1: Core tenant isolation (domains, APIs, CLI).
    • Phase 2: Advanced features (tenant-specific facades, dynamic subdomains, billing integration).
    • Phase 3: Scalability optimizations (e.g., tenant-aware caching, connection pooling).
  • Use Cases:
    • B2B SaaS: Isolate customer data (e.g., CRM, project management tools).
    • Marketplaces: Separate vendor/buyer data (e.g., e-commerce, freelance platforms).
    • Regional Compliance: GDPR/CCPA isolation by tenant (e.g., EU vs. US data).
    • Internal Tools: Departmental portals (e.g., HR, finance) with shared codebases.

When to Consider This Package

Adopt This Package If:

  • Your Laravel app requires tenant isolation (data, configurations, or UI) without heavy customization.
  • You need subdomain-based routing (e.g., tenant1.app.com) or API-based tenant resolution (e.g., X-Tenant-ID header).
  • Shared schema or database-per-tenant architectures are viable, but you want to avoid manual connection switching.
  • You prioritize developer velocity over ultra-custom tenant logic (e.g., no need for tenant-specific Eloquent models).
  • Your team lacks deep Laravel internals expertise but needs batteries-included multitenancy (e.g., tenant-aware jobs, Artisan commands).
  • You’re using Laravel 11+ and PHP 8.2+ (hard requirements).

Look Elsewhere If:

  • You need tenant-specific Eloquent models (this package uses a generic IsTenant contract; alternatives like tenancy/tenancy offer tighter model integration).
  • Your multitenancy requires complex routing (e.g., path-based tenants like /tenant1/dashboard); consider gecche/laravel-multidomain.
  • You’re building a highly dynamic tenant system (e.g., runtime tenant creation/deletion) with strict performance SLAs (this package is unopinionated but not optimized for edge cases).
  • Your app uses non-Laravel services that can’t coexist with tenant isolation (e.g., legacy monoliths).
  • You need built-in billing/subscription management (combine with Stripe/Lemon Squeezy or use tenancy/tenancy).
  • Your team prefers opinionated frameworks (e.g., tenancy/tenancy provides more scaffolding for common SaaS patterns).

How to Pitch It (Stakeholders)

For Executives:

"This package lets us launch a multi-tenant SaaS platform without building multitenancy from scratch—saving 3–6 months of dev time. It handles the heavy lifting:

  • Automatically routes users to their isolated tenant (e.g., acme.app.com → Acme’s data only).
  • Isolates databases or schemas per tenant with a single config line.
  • Ensures jobs, CLI commands, and APIs respect tenant boundaries.
  • Scales with our growth—no refactoring as we add features like tenant-specific branding. We’re choosing this over custom code because it’s battle-tested (1.3K+ stars, used by teams like [Example Company]) and MIT-licensed, so we own our stack. Alternatives like tenancy/tenancy offer more scaffolding but lock us into their roadmap. This gives us flexibility to customize later."

ROI:

  • Time: 70% faster than custom multitenancy.
  • Risk: Proven in production; Spatie’s support for edge cases.
  • Cost: Free (MIT license); no vendor lock-in.

For Engineering:

"This is a minimalist, contract-based multitenancy solution for Laravel. Here’s why it’s a great fit:

  1. Unopinionated: We control tenant resolution (domains, headers, DB queries) via custom TenantFinder classes.
  2. Database Agnostic: Works with single DB (shared schema) or multi-DB setups.
  3. Tenant-Aware by Default:
    • Jobs: Mark classes with TenantAware or configure globally.
    • Artisan: Run commands per tenant with tenant:artisan.
    • Facades: Clear cached instances on tenant switch (via custom tasks).
  4. Performance: Lightweight (~500 LOC core); no global overhead.
  5. Extensible:
    • Add tasks for tenant-specific setup (e.g., load tenant configs).
    • Override default behavior (e.g., custom tenant models via IsTenant contract).

Trade-offs vs. Alternatives:

Feature spatie/laravel-multitenancy tenancy/tenancy
Tenant-specific models ❌ (Generic IsTenant) ✅ (Built-in)
Opinionated scaffolding ✅ (Migrations, seeds)
Customization ✅ (Contracts, tasks) ⚠️ (Limited)
Learning curve Low Medium

Recommendation: Use this for core isolation and pair with:

  • laravel-breeze/inertiajs for tenant-specific UIs.
  • spatie/laravel-permission for role-based access control (RBAC) per tenant.
  • Custom middleware to enforce tenant-specific logic (e.g., feature flags).

Next Steps:

  1. Spike: Implement domain-based tenant resolution in 2 hours (see DomainTenantFinder).
  2. Architecture Decision: Choose shared schema vs. multi-DB based on data size/compliance.
  3. Integrate: Add tenant-aware jobs and Artisan tasks in the next sprint."*
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
codraw/framework-extra-bundle
codraw/messenger
codraw/security
codraw/mailer
codraw/contracts
codraw/profiling
codraw/dependency-injection
codraw/tester
codraw/core
nexmo/api-specification
capell-app/block-library
axium/identity
cetria/laravel-dummy-models
cetria/reflection-helper
agropredict/sso-auth-bundle
evolvestudio/spam-protection
datacore/hub-sdk
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony