PHPackages                             stefanbauer/landlord-extended - 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. [Database &amp; ORM](/categories/database)
4. /
5. stefanbauer/landlord-extended

ActiveLibrary[Database &amp; ORM](/categories/database)

stefanbauer/landlord-extended
=============================

A simple, single database multi-tenancy solution for Laravel 5.2+

v2.12.0(5y ago)101.2k1MITPHPPHP &gt;=5.6.0CI failing

Since Jan 18Pushed 5y ago1 watchersCompare

[ Source](https://github.com/stefanbauer/landlord-extended)[ Packagist](https://packagist.org/packages/stefanbauer/landlord-extended)[ RSS](/packages/stefanbauer-landlord-extended/feed)WikiDiscussions master Synced 4w ago

READMEChangelog (4)Dependencies (4)Versions (18)Used By (0)

Landlord for Laravel &amp; Lumen 5.2+
=====================================

[](#landlord-for-laravel--lumen-52)

[![Latest Version on Packagist](https://camo.githubusercontent.com/f630a3b69c36fd0f5902acb1f61409592c9a724c5ba4843eef2a7f95e9a5031f/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f73746566616e62617565722f6c616e646c6f72642d657874656e6465642e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/stefanbauer/landlord-extended)[![Build Status](https://camo.githubusercontent.com/7dbe162fd7420432d488923c547785d1b379f51b97b7d06cb8884abe1428eef5/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f73746566616e62617565722f6c616e646c6f72642d657874656e6465642f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/stefanbauer/landlord-extended)[![Total Downloads](https://camo.githubusercontent.com/62c7c00ddc9cb2134a7571517ec0964788d3e2d1ef7148bbec3f25b56dfad124/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f73746566616e62617565722f6c616e646c6f72642d657874656e6465642e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/stefanbauer/landlord-extended)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)

This package based on [HipsterJazzbo/Landlord](https://github.com/HipsterJazzbo/Landlord), a single database multi-tenancy package for Laravel &amp; Lumen 5.2+. Still the same, but maintaining the dependencies.

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

[](#installation)

To get started, require the package by:

```
composer require stefanbauer/landlord-extended
```

Laravel
-------

[](#laravel)

### Version 5.5+

[](#version-55)

Laravel &gt;= 5.5 uses Package Auto-Discovery, so it doesn't require you to manually add the ServiceProvider. If you still have to, here's how to:

### Version 5.4 and below

[](#version-54-and-below)

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

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

Register the Facade if you’d like:

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

You could also publish the config file:

```
php artisan vendor:publish --provider="StefanBauer\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(StefanBauer\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 StefanBauer\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 StefanBauer\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

34

—

LowBetter than 74% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity21

Limited adoption so far

Community18

Small or concentrated contributor base

Maturity67

Established project with proven stability

 Bus Factor2

2 contributors hold 50%+ of commits

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 ~108 days

Recently: every ~279 days

Total

17

Last Release

2135d ago

Major Versions

v1.0.2 → v2.0-beta2016-09-27

PHP version history (2 changes)v1.0PHP &gt;=5.4.0

v2.0-betaPHP &gt;=5.6.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/fae65091ef4f6c3a2d4e0624f7d21783acca3268448be6bef9dc1e2567045c56?d=identicon)[stefanbauer](/maintainers/stefanbauer)

---

Top Contributors

[![jbrooksuk](https://avatars.githubusercontent.com/u/246103?v=4)](https://github.com/jbrooksuk "jbrooksuk (12 commits)")[![stefanbauer](https://avatars.githubusercontent.com/u/3192662?v=4)](https://github.com/stefanbauer "stefanbauer (11 commits)")[![geomagilles](https://avatars.githubusercontent.com/u/134669?v=4)](https://github.com/geomagilles "geomagilles (4 commits)")[![phaberest](https://avatars.githubusercontent.com/u/3464092?v=4)](https://github.com/phaberest "phaberest (2 commits)")[![vernesto84](https://avatars.githubusercontent.com/u/662250?v=4)](https://github.com/vernesto84 "vernesto84 (2 commits)")[![bissolli](https://avatars.githubusercontent.com/u/1808444?v=4)](https://github.com/bissolli "bissolli (1 commits)")[![renanwilliam](https://avatars.githubusercontent.com/u/19995615?v=4)](https://github.com/renanwilliam "renanwilliam (1 commits)")[![rikvdlooi](https://avatars.githubusercontent.com/u/15796734?v=4)](https://github.com/rikvdlooi "rikvdlooi (1 commits)")[![tonydew](https://avatars.githubusercontent.com/u/525153?v=4)](https://github.com/tonydew "tonydew (1 commits)")[![Omranic](https://avatars.githubusercontent.com/u/406705?v=4)](https://github.com/Omranic "Omranic (1 commits)")[![christian-thomas](https://avatars.githubusercontent.com/u/740172?v=4)](https://github.com/christian-thomas "christian-thomas (1 commits)")[![GrahamCampbell](https://avatars.githubusercontent.com/u/2829600?v=4)](https://github.com/GrahamCampbell "GrahamCampbell (1 commits)")[![jkniest](https://avatars.githubusercontent.com/u/15618191?v=4)](https://github.com/jkniest "jkniest (1 commits)")[![luizreginaldo](https://avatars.githubusercontent.com/u/1282758?v=4)](https://github.com/luizreginaldo "luizreginaldo (1 commits)")

---

Tags

tenantmultitenancytenancymultitenant

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/stefanbauer-landlord-extended/health.svg)

```
[![Health](https://phpackages.com/badges/stefanbauer-landlord-extended/health.svg)](https://phpackages.com/packages/stefanbauer-landlord-extended)
```

###  Alternatives

[hipsterjazzbo/landlord

A simple, single database multi-tenancy solution for Laravel 5.2+

612271.9k1](/packages/hipsterjazzbo-landlord)[nunomazer/laravel-samehouse

A multi-tenant Laravel package, based on single database, simple and ease to use

24258.8k](/packages/nunomazer-laravel-samehouse)

PHPackages © 2026

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