Looking to hire Laravel developers? Try LaraJobs

multitenancy-laravel maintained by ronu

Description
AppContext-integrated multi-tenancy for Laravel.
Last update
2026/02/10 22:27 (dev-main)
License
Links
Downloads
0

Comments
comments powered by Disqus

Laravel Multi-Tenancy (AppContext-based)

A lightweight multi-tenancy layer designed to integrate with ronu/laravel-app-context. It provides global tenant scoping, tenant-aware model behavior, and middleware validation, with minimal overhead when disabled.

Key Features

  • Global tenant scope for Eloquent models
  • Tenant-aware model creation and updates
  • Per-channel enable/disable based on AppContext channels
  • Auto-detection of tenancy based on channel configuration
  • Strict, soft, or disabled enforcement modes
  • Optional bypass mode for superusers
  • Artisan command to inspect status

Requirements

  • PHP 8.1+
  • Laravel 9, 10, or 11
  • ronu/laravel-app-context

Installation

  1. Require the AppContext package.
  2. Require this package (or include it as a local package).
  3. Register the service provider if you are not using auto-discovery.
  4. Publish the config file.
php artisan vendor:publish --tag=tenancy-config

Configuration

Main config file: config/tenancy.php

Environment variables (from .env.example):

  • TENANCY_ENABLED true | false | empty (auto-detect)
  • TENANCY_COLUMN default tenant column, e.g. tenant_id
  • TENANCY_ENFORCEMENT_MODE strict | soft | disabled
  • TENANCY_ADMIN_ENABLED override per channel
  • TENANCY_MOBILE_ENABLED override per channel
  • TENANCY_SITE_ENABLED override per channel
  • TENANCY_PARTNER_ENABLED override per channel
  • TENANCY_LOG_BYPASSES true | false
  • TENANCY_LOG_CONTEXT_CHANGES true | false
  • TENANCY_ALERT_VIOLATIONS true | false

AppContext Integration

This package reads the current channel and tenant from AppContext:

  • app-context.channels.{channel}.tenant_mode determines default behavior.
  • TenantOwnershipValidator hydrates the tenant context from AppContext.
  • TenantScope applies tenant filtering automatically.

Usage

Apply tenant behavior to a model

use Ronu\MultitenancyLaravel\Traits\TenantAware;

class Order extends Model
{
    use TenantAware;
}

Tenant scope helpers

Order::query()->withoutTenantScope()->get();
Order::query()->forTenant($tenantId)->get();

Middleware

Register TenantOwnershipValidator after ctx.auth / ctx.bind so it can read AppContext.

Status command

php artisan tenancy:status
php artisan tenancy:status --detailed

Security Notes

  • Cross-tenant operations require a superuser resolver.
  • Tenant ID modification on updates is blocked by TenantAware.

Known Issues (Resolved)

  • Namespace mismatches were standardized to Ronu\\MultitenancyLaravel.

License

MIT