PHPackages                             acdphp/laravel-multitenancy - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Framework](/categories/framework)
4. /
5. acdphp/laravel-multitenancy

ActiveLibrary[Framework](/categories/framework)

acdphp/laravel-multitenancy
===========================

Laravel multi-tenancy model scoping and automatic tenancy assignment.

v2.6.0(2w ago)82.7k↑592.9%1MITPHPPHP ^8.0CI passing

Since Oct 6Pushed 2mo agoCompare

[ Source](https://github.com/acdphp/laravel-multitenancy)[ Packagist](https://packagist.org/packages/acdphp/laravel-multitenancy)[ RSS](/packages/acdphp-laravel-multitenancy/feed)WikiDiscussions main Synced yesterday

READMEChangelog (10)Dependencies (24)Versions (16)Used By (0)

Laravel Multitenancy
====================

[](#laravel-multitenancy)

[![Latest Stable Version](https://camo.githubusercontent.com/af36fde16b1283962bcf35cc71fe664066e421d9c6477464af9f89936acd7a91/68747470733a2f2f706f7365722e707567782e6f72672f6163647068702f6c61726176656c2d6d756c746974656e616e63792f76)](https://packagist.org/packages/acdphp/laravel-multitenancy)

Laravel multi-tenancy model scoping and automatic tenancy assignment.

Installation
------------

[](#installation)

```
composer require acdphp/laravel-multitenancy
```

Configuration
-------------

[](#configuration)

Publish config

```
php artisan vendor:publish --provider="Acdphp\Multitenancy\TenancyServiceProvider"
```

Change the column name to look for tenancy in models.

```
'tenant_ref_key' => 'company_id',
```

By default, the tenant id is automatically resolved using the authenticated user's tenant ref key defined above. You can disable this and [define your own resolver logic](#manually-setting-the-tenant) in AppServiceProvider or any service provider.

```
'auto_resolve_tenant_id' => true,
```

By default, the tenant id is automatically assigned during creation using the resolved tenancy defined above. You can disable this and manually set tenant id as you normally would.

```
'auto_assign_tenant_id' => true,
```

Example Usage
-------------

[](#example-usage)

```
use \Acdphp\Multitenancy\Traits\BelongsToTenant;

class Site extends Model
{
    use BelongsToTenant;

    protected $fillable = [
        'company_id',
        ...
    ];
}
```

Per-model column name override
------------------------------

[](#per-model-column-name-override)

The `tenant_ref_key` config defines the column name globally. You can override it for a specific model by defining the `$tenantRefKey` property. This affects auto-assignment on creation and all scoping queries for that model.

```
use \Acdphp\Multitenancy\Traits\BelongsToTenant;

class Site extends Model
{
    use BelongsToTenant;

    protected string $tenantRefKey = 'org_id'; // Override the global tenant_ref_key for this model

    protected $fillable = [
        'org_id',
        ...
    ];
}
```

Scoping from parent relationship
--------------------------------

[](#scoping-from-parent-relationship)

```
use \Acdphp\Multitenancy\Traits\BelongsToTenant;

class Product extends Model
{
    use BelongsToTenant;

    protected $fillable = [
        'site_id',
        ...
    ];

    protected string $scopeTenancyFromRelation = 'site'; // Define to scope from parent model

    public function site(): BelongsTo
    {
        return $this->belongsTo(Site::class);
    }
}
```

---

Manually setting the tenant
---------------------------

[](#manually-setting-the-tenant)

In the registration, for example, tenancy isn't set because it's a non-authenticated endpoint. The tenant has to be manually assigned.

```
use Acdphp\Multitenancy\Facades\Tenancy;

// Create a company and set it as tenant
$company = Company::create(...);
Tenancy::setTenantIdResolver(fn () => $company->id);

// Then proceed to create a user
User::create(...);
```

Custom Scoping Tenant ID
------------------------

[](#custom-scoping-tenant-id)

By default, the scoping uses the resolved tenant ID. You can set a custom scoping tenant ID resolver that supports a single value or an array of IDs.

```
use Acdphp\Multitenancy\Facades\Tenancy;

// Scope to a single tenant
Tenancy::setScopingTenantIdResolver(fn () => $tenantId);

// Scope to multiple tenants
Tenancy::setScopingTenantIdResolver(fn () => [1, 2, 3]);
```

Bypassing Scope
---------------

[](#bypassing-scope)

Sometimes, it's needed to bypass scoping when accessing a model that belongs to a tenant.

```
use Acdphp\Multitenancy\Facades\Tenancy;

Tenancy::bypassScope();
```

Or by using the middleware.

```
Route::middleware(['tenancy.scope.bypass'])->get('/resources/all', ...);
```

To re-enable scoping after bypassing:

```
Tenancy::resetScopeBypass();
```

Bypassing Automatic Tenancy Assignment
--------------------------------------

[](#bypassing-automatic-tenancy-assignment)

It's also possible to bypass auto-tenancy assignments.

```
use Acdphp\Multitenancy\Facades\Tenancy;

Tenancy::bypassCreating();
```

Or by using the middleware.

```
Route::middleware(['tenancy.creating.bypass'])->post('your-route', ...);
```

To re-enable auto-assignment after bypassing:

```
Tenancy::resetCreatingBypass();
```

---

Testing
-------

[](#testing)

```
composer test

# with docker-compose
docker-compose run --rm test-php84
```

Linting and static analysis
---------------------------

[](#linting-and-static-analysis)

```
composer lint

# with docker-compose
docker-compose run --rm lint
```

Lint Fix
--------

[](#lint-fix)

```
composer lint-fix

# with docker-compose
docker-compose run --rm lint-fix
```

---

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE) for more information.

###  Health Score

49

—

FairBetter than 94% of packages

Maintenance90

Actively maintained with recent releases

Popularity28

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity56

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~70 days

Recently: every ~45 days

Total

15

Last Release

16d ago

Major Versions

1.0.1 → v2.0.02023-12-28

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/145334055?v=4)[Acdphp](/maintainers/acdphp)[@acdphp](https://github.com/acdphp)

---

Top Contributors

[![cdinopol](https://avatars.githubusercontent.com/u/49012962?v=4)](https://github.com/cdinopol "cdinopol (17 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StyleLaravel Pint

Type Coverage Yes

### Embed Badge

![Health badge](/badges/acdphp-laravel-multitenancy/health.svg)

```
[![Health](https://phpackages.com/badges/acdphp-laravel-multitenancy/health.svg)](https://phpackages.com/packages/acdphp-laravel-multitenancy)
```

###  Alternatives

[livewire/livewire

A front-end framework for Laravel.

23.6k89.0M2.7k](/packages/livewire-livewire)[psalm/plugin-laravel

Psalm plugin for Laravel

3355.3M345](/packages/psalm-plugin-laravel)[laravel/ai

The official AI SDK for Laravel.

1.0k3.2M194](/packages/laravel-ai)[api-platform/laravel

API Platform support for Laravel

58171.5k14](/packages/api-platform-laravel)[fleetbase/core-api

Core Framework and Resources for Fleetbase API

1235.9k20](/packages/fleetbase-core-api)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
