2017 © Pedro Peláez
 

library laravel-multi-tenant

image

aura-is-here/laravel-multi-tenant

  • Monday, February 1, 2016
  • by HipsterJazzbo
  • Repository
  • 27 Watchers
  • 144 Stars
  • 11,996 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 48 Forks
  • 2 Open issues
  • 5 Versions
  • 5 % Grown

The README.md

Laravel Multi Tenant




NOTE: THIS PACKAGE HAS MOVED

This package is now Laravel 5.2+ only, and has moved to https://github.com/HipsterJazzbo/Landlord., (*1)

This version will no longer be maintained, but will stay here for legacy compatibility., (*2)




, (*3)

A general purpose multi-tenancy package for Laravel 5.2+. Accidentally derived from the work of @tonydew, and with help from @rizqidjamaluddin, (*4)

Installation

To get started, require this package in your composer.json and run composer update:, (*5)

"aura-is-here/laravel-multi-tenant": "dev-master"

After updating composer, add the ServiceProvider to the providers array in app/config/app.php:, (*6)

AuraIsHere\LaravelMultiTenant\LaravelMultiTenantServiceProvider::class,

You'll probably want to set up the alias:, (*7)

'TenantScope' => AuraIsHere\LaravelMultiTenant\Facades\TenantScopeFacade::class

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

php artisan vendor:publish --provider="AuraIsHere\LaravelMultiTenant\LaravelMultiTenantServiceProvider"

and set up your tenant_column setting, if you have an app-wide default., (*9)

Usage

First off, this package assumes that you have a column on all of your tenant-scoped tables that references which tenant each row belongs to., (*10)

For example, you might have a companies table, and all your other tables might have a company_id column (with a foreign key, right?)., (*11)

Next, you'll have to call TenantScope::addTenant($tenantColumn, $tenantId). It doesn't matter where, as long as it happens on every request. This is important; if you only set the tenant in your login method for example, that won't run for subsequent requests and queries will no longer be scoped., (*12)

Some examples of good places to call TenantScope::addTenant($tenantColumn, $tenantId) might be:, (*13)

  • In a global Middleware
  • In an oauth system, wherever you're checking the token on each request
  • In the constructor of a base controller

Once you've got that all worked out, simply use the trait in all your models that you'd like to scope by tenant:, (*14)

<?php

use AuraIsHere\LaravelMultiTenant\Traits\TenantScopedModelTrait;

class Model extends Eloquent {

    use TenantScopedModelTrait;
}

Henceforth, all operations against that model will be scoped automatically., (*15)

You can also set a $tenantColumns property on the model to override the tenants applicable to that model., (*16)

$models = Model::all(); // Only the Models with the correct tenant id

$model = Model::find(1); // Will fail if the Model with `id` 1 belongs to a different tenant

$newModel = Model::create(); // Will have the tenant id added automatically

If you need to run queries across all tenants, you can do it easily:, (*17)

$allModels = Model::allTenants()->get(); //You can run any fluent query builder methods here, and they will not be scoped by tenant

When you are developing a multi tenanted application, it can be confusing sometimes why you keep getting ModelNotFound exceptions., (*18)

Laravel Multi Tenant will catch those exceptions, and re-throw them as ModelNotFoundForTenant, to help you out :), (*19)

Contributing

Please! This is not yet a complete solution, but there's no point in all of us re-inventing this wheel over and over. If you find an issue, or have a better way to do something, open an issue or a pull request., (*20)

The Versions

01/02 2016

dev-master

9999999-dev

  Sources   Download

The Requires

 

The Development Requires

by Caleb Fidecaro

19/01 2016

v0.1.0

0.1.0.0

  Sources   Download

The Requires

 

The Development Requires

by Caleb Fidecaro

06/11 2014

dev-laravel-4.2

dev-laravel-4.2

  Sources   Download

The Requires

 

The Development Requires

by Caleb Fidecaro

06/11 2014

0.1.0-RC2

0.1.0.0-RC2

  Sources   Download

The Requires

 

The Development Requires

by Caleb Fidecaro

27/10 2014

0.1.0-RC1

0.1.0.0-RC1

  Sources   Download

The Requires

 

The Development Requires

by Caleb Fidecaro