2017 © Pedro PelĂĄez
 

library landlord

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

image

betalabs/landlord

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

  • Saturday, March 17, 2018
  • by diego-betalabs
  • Repository
  • 2 Watchers
  • 2 Stars
  • 241 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 98 Forks
  • 0 Open issues
  • 13 Versions
  • 20 % Grown

The README.md

Landlord for Laravel & Lumen 5.2+

Landlord for Laravel & Lumen 5.2+, (*1)

StyleCI Status
Build Status, (*2)

Based on HipsterJazzbo/Landlord, a single database multi-tenancy package for Laravel & Lumen 5.2+., (*3)

Installation

To get started, require this package:, (*4)

composer require victormacedo/landlord

Laravel

Add the ServiceProvider in config/app.php:, (*5)

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

Register the Facade if you’d like:, (*6)

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

You could also publish the config file:, (*7)

php artisan vendor:publish --provider="HipsterJazzbo\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., (*8)

Lumen

You'll need to set the service provider in your bootstrap/app.php:, (*9)

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

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

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., (*11)

For example, you might have a companies table, and a bunch of other tables that have a company_id column., (*12)

Adding and Removing Tenants

IMPORTANT NOTE: Landlord is stateless. This means that when you call addTenant(), it will only scope the current request., (*13)

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., (*14)

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()., (*15)

You can pass in either a tenant column and id:, (*16)

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

Or an instance of a Tenant model:, (*17)

$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., (*18)

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

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

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:, (*21)

// 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:, (*22)

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

Setting up your Models

To set up a model to be scoped automatically, simply use the BelongsToTenants trait:, (*23)


use Illuminate\Database\Eloquent\Model; use HipsterJazzbo\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:, (*24)


use Illuminate\Database\Eloquent\Model; use HipsterJazzbo\Landlord\BelongsToTenants; class ExampleModel extends Model { use BelongsToTenants; public $tenantColumns = ['tenant_id']; }

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:, (*25)

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

Querying Tenant scoped Models

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

// 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., (*27)

Landlord will catch those exceptions, and re-throw them as ModelNotFoundForTenantException, to help you out :), (*28)

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

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

Under the hood, Landlord uses Laravel's anonymous 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:, (*30)

// 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

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

The Versions

17/03 2018

dev-master

9999999-dev

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

  Sources   Download

MIT

The Requires

 

The Development Requires

by Caleb Fidecaro
by Victor Macedo

tenant multitenant tenancy multitenancy

17/03 2018

v2.3

2.3.0.0

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

  Sources   Download

MIT

The Requires

 

The Development Requires

by Caleb Fidecaro
by Victor Macedo

tenant multitenant tenancy multitenancy

12/09 2017

v2.2

2.2.0.0

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

  Sources   Download

MIT

The Requires

 

The Development Requires

by Caleb Fidecaro
by Victor Macedo

tenant multitenant tenancy multitenancy

23/11 2016

v2.0.1.4

2.0.1.4

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

  Sources   Download

MIT

The Requires

 

The Development Requires

by Caleb Fidecaro
by Victor Macedo

tenant multitenant tenancy multitenancy

10/11 2016

v2.0.1.3

2.0.1.3

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

  Sources   Download

MIT

The Requires

 

The Development Requires

by Caleb Fidecaro
by Victor Macedo

tenant multitenant tenancy multitenancy

09/11 2016

v2.0.1.2

2.0.1.2

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

  Sources   Download

MIT

The Requires

 

The Development Requires

by Caleb Fidecaro
by Victor Macedo

tenant multitenant tenancy multitenancy

09/11 2016

v2.0.1.1

2.0.1.1

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

  Sources   Download

MIT

The Requires

 

The Development Requires

by Caleb Fidecaro
by Victor Macedo

tenant multitenant tenancy multitenancy

08/11 2016

v2.0.1

2.0.1.0

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

  Sources   Download

MIT

The Requires

 

The Development Requires

by Caleb Fidecaro

tenant multitenant tenancy multitenancy

31/10 2016

v2.0

2.0.0.0

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

  Sources   Download

MIT

The Requires

 

The Development Requires

by Caleb Fidecaro

tenant multitenant tenancy multitenancy

27/09 2016

v2.0-beta

2.0.0.0-beta

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

  Sources   Download

MIT

The Requires

 

The Development Requires

by Caleb Fidecaro

tenant multitenant tenancy multitenancy

09/09 2016

v1.0.2

1.0.2.0

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

  Sources   Download

MIT

The Requires

 

The Development Requires

by Caleb Fidecaro

tenant multitenant tenancy multitenancy

18/01 2016

v1.0.1

1.0.1.0

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

  Sources   Download

MIT

The Requires

 

The Development Requires

by Caleb Fidecaro

tenant multitenant tenancy multitenancy

18/01 2016

v1.0

1.0.0.0

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

  Sources   Download

MIT

The Requires

 

The Development Requires

by Caleb Fidecaro

tenant multitenant tenancy multitenancy