PHPackages                             airdjura/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. airdjura/laravel-multitenancy

ActiveLibrary[Framework](/categories/framework)

airdjura/laravel-multitenancy
=============================

A simple, single database multi-tenancy solution for Laravel

110PHPCI failing

Since Aug 27Pushed 3y agoCompare

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

READMEChangelogDependenciesVersions (1)Used By (0)

Landlord for Laravel
====================

[](#landlord-for-laravel)

Single database multi-tenancy package for Laravel

[![Build Status](https://camo.githubusercontent.com/ed84b30b1c2dd958f0d6cbf0b4edac9872ef8d43583743c721b592b2315b0878/68747470733a2f2f7472617669732d63692e6f72672f626973736f6c6c692f6c61726176656c2d6c616e646c6f72642e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/bissolli/laravel-landlord)[![Latest Stable Version](https://camo.githubusercontent.com/41d103dd1f5cc9a4e76c36c6581645f8fe82faa51bc32b19adc0a8cfd15947a4/68747470733a2f2f706f7365722e707567782e6f72672f626973736f6c6c692f6c61726176656c2d6c616e646c6f72642f762f737461626c65)](https://packagist.org/packages/bissolli/laravel-landlord)[![Total Downloads](https://camo.githubusercontent.com/7a67130dd3b678ae190a67fe95a86f2b7cbdcf3b9f7b7c5c72cd02f01189f57b/68747470733a2f2f706f7365722e707567782e6f72672f626973736f6c6c692f6c61726176656c2d6c616e646c6f72642f646f776e6c6f616473)](https://packagist.org/packages/bissolli/laravel-landlord)[![License](https://camo.githubusercontent.com/2835eb06de91665a91057489741fb3fb5640ebc94adff516ebddb0cf7ce4f6b6/68747470733a2f2f706f7365722e707567782e6f72672f626973736f6c6c692f6c61726176656c2d6c616e646c6f72642f6c6963656e7365)](https://packagist.org/packages/bissolli/laravel-landlord)

> Based on [HipsterJazzbo/Landlord](https://github.com/HipsterJazzbo/Landlord).

> **Upgrading from Landlord v1?** Make sure to read the [change log](CHANGELOG.md) to see what needs updating.

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

[](#installation)

To get started, require this package:

```
composer require airdjura/laravel-multitenancy
```

### Laravel

[](#laravel)

Add the ServiceProvider in `config/app.php`:

```
    'providers' => [
        ...
        airDjura\Landlord\LandlordServiceProvider::class,
    ],
```

Register the Facade if you’d like:

```
    'aliases' => [
        ...
        'Landlord'   => airDjura\Landlord\Facades\Landlord::class,
    ],
```

You could also publish the config file:

```
php artisan vendor:publish --provider="airDjura\Landlord\LandlordServiceProvider"
```

and set your `default_tenant_columns` setting, if you have an app-wide default. LandLord will use this setting to scope models that don’t have a `$tenantColumns` property set.

### Lumen

[](#lumen)

You'll need to set the service provider in your `bootstrap/app.php`:

```
$app->register(airDjura\Landlord\LandlordServiceProvider::class);
```

And make sure you've un-commented `$app->withEloquent()`.

Usage
-----

[](#usage)

This package assumes that you have at least one column on all of your Tenant scoped tables that references which tenant each row belongs to.

For example, you might have a `companies` table, and a bunch of other tables that have a `company_id` column.

### Adding and Removing Tenants

[](#adding-and-removing-tenants)

> **IMPORTANT NOTE:** Landlord is stateless. This means that when you call `addTenant()`, it will only scope the *current request*.
>
> Make sure that you are adding your tenants in such a way that it happens on every request, and before you need Models scoped, like in a middleware or as part of a stateless authentication method like OAuth.

You can tell Landlord to automatically scope by a given Tenant by calling `addTenant()`, either from the `Landlord` facade, or by injecting an instance of `TenantManager()`.

You can pass in either a tenant column and id:

```
Landlord::addTenant('tenant_id', 1);
```

Or an instance of a Tenant model:

```
$tenant = Tenant::find(1);

Landlord::addTenant($tenant);
```

If you pass a Model instance, Landlord will use Eloquent’s `getForeignKey()` method to decide the tenant column name.

You can add as many tenants as you need to, however Landlord will only allow **one** of each type of tenant at a time.

To remove a tenant and stop scoping by it, simply call `removeTenant()`:

```
Landlord::removeTenant('tenant_id');

// Or you can again pass a Model instance:
$tenant = Tenant::find(1);

Landlord::removeTenant($tenant);
```

You can also check whether Landlord currently is scoping by a given tenant:

```
// As you would expect by now, $tenant can be either a string column name or a Model instance
Landlord::hasTenant($tenant);
```

And if for some reason you need to, you can retrieve Landlord's tenants:

```
// $tenants is a Laravel Collection object, in the format 'tenant_id' => 1
$tenants = Landlord::getTenants();
```

### Setting up your Models

[](#setting-up-your-models)

To set up a model to be scoped automatically, simply use the `BelongsToTenants` trait:

```
use Illuminate\Database\Eloquent\Model;
use airDjura\Landlord\BelongsToTenants;

class ExampleModel extends Model
{
    use BelongsToTenants;
}
```

If you’d like to override the tenants that apply to a particular model, you can set the `$tenantColumns` property:

```
use Illuminate\Database\Eloquent\Model;
use airDjura\Landlord\BelongsToTenants;

class ExampleModel extends Model
{
    use BelongsToTenants;

    public $tenantColumns = ['tenant_id'];
}
```

### Creating new Tenant scoped Models

[](#creating-new-tenant-scoped-models)

When you create a new instance of a Model which uses `BelongsToTenants`, Landlord will automatically add any applicable Tenant ids, if they are not already set:

```
// 'tenant_id' will automatically be set by Landlord
$model = ExampleModel::create(['name' => 'whatever']);
```

### Querying Tenant scoped Models

[](#querying-tenant-scoped-models)

After you've added tenants, all queries against a Model which uses `BelongsToTenant` will be scoped automatically:

```
// This will only include Models belonging to the current tenant(s)
ExampleModel::all();

// This will fail with a ModelNotFoundForTenantException if it belongs to the wrong tenant
ExampleModel::find(2);
```

> **Note:** When you are developing a multi tenanted application, it can be confusing sometimes why you keep getting `ModelNotFound` exceptions for rows that DO exist, because they belong to the wrong tenant.
>
> Landlord will catch those exceptions, and re-throw them as `ModelNotFoundForTenantException`, to help you out :)

If you need to query across all tenants, you can use `allTenants()`:

```
// Will include results from ALL tenants, just for this query
ExampleModel::allTenants()->get()
```

Under the hood, Landlord uses Laravel's [anonymous global scopes](https://laravel.com/docs/5.3/eloquent#global-scopes). This means that if you are scoping by multiple tenants simultaneously, and you want to exclude one of the for a single query, you can do so:

```
// Will not scope by 'tenant_id', but will continue to scope by any other tenants that have been set
ExampleModel::withoutGlobalScope('tenant_id')->get();
```

Contributing
------------

[](#contributing)

If you find an issue, or have a better way to do something, feel free to open an issue or a pull request.

###  Health Score

16

—

LowBetter than 5% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity25

Early-stage or recently created project

 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.

### Community

Maintainers

![](https://www.gravatar.com/avatar/5e7e222ed61adc7f7ca374296bdfedf820ad1fd7e5df465d4bb38ffcbfb60a51?d=identicon)[airDjura](/maintainers/airDjura)

---

Top Contributors

[![airDjura](https://avatars.githubusercontent.com/u/22389169?v=4)](https://github.com/airDjura "airDjura (7 commits)")

### Embed Badge

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

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

###  Alternatives

[laravel/telescope

An elegant debug assistant for the Laravel framework.

5.2k67.8M192](/packages/laravel-telescope)[spiral/roadrunner

RoadRunner: High-performance PHP application server and process manager written in Go and powered with plugins

8.4k12.2M84](/packages/spiral-roadrunner)[nolimits4web/swiper

Most modern mobile touch slider and framework with hardware accelerated transitions

41.8k177.2k1](/packages/nolimits4web-swiper)[laravel/dusk

Laravel Dusk provides simple end-to-end testing and browser automation.

1.9k36.7M259](/packages/laravel-dusk)[laravel/prompts

Add beautiful and user-friendly forms to your command-line applications.

708181.8M596](/packages/laravel-prompts)[cakephp/chronos

A simple API extension for DateTime.

1.4k47.7M121](/packages/cakephp-chronos)

PHPackages © 2026

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