Download the PHP package globecode/laravel-multitenant without Composer
On this page you can find all versions of the php package globecode/laravel-multitenant. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download globecode/laravel-multitenant
More information about globecode/laravel-multitenant
Files in globecode/laravel-multitenant
Informations about the package laravel-multitenant
Laravel Multi-Tenant
Sep 29, 2014: WIP for release as an L4 package.
Installation
-
Require this package in your
composer.json
file in the"require"
block: -
Add the service provider to the providers array in
app/config/app.php
. This is only used for the "Publishconfig
" command two steps down: -
cd
into your project directory and update via Composer: -
Publish the
config
to your application. This allows you to change the name of the Tenant column name in your schema. Aconfig
file will be installed toapp/config/packages/globecode/laravel-multitenant/
which you can edit: -
Create and run new migrations to setup the necessary schema. Example migrations are provided in the Package
migrations
folder. -
Add the
getTenantId()
method to yourUser
(and any other "scoped" models) or just once in aBaseModel
(recommended): - Optional Global Override. If you want to globally override scope, such as for an Admin, then add the
isAdmin()
method to yourUser
model. TheScopedByTenant
trait will look for this method and automatically override the scope on all queries if this method returnstrue
:
Usage
-
Scope a model using the
ScopedByTenant
trait to make all queries on that model globally scoped to a Tenant. Never worry about accidentally querying outside a Tenant's data! -
Globally removing scope:
A: Globally remove scope from a Controller, such as in an Admin situation:
Or
B: Globally remove scope by using the
Auth
check in theScopedByTenant
trait'sbootTenantId()
method. See the instructions in the Setup -> Optional Global Override section above.
Note: You can use any of the public
methods on the ScopedByTenant
trait in your models and controllers.
Repositories
If you use repositories, you will need to build queries off of the TenantScope
class instead of the ScopedByTenant
trait. If you look at the TenantScope
class you will see there are extensions to \Illuminate\Database\Query\Builder, these are methods available to your repositories; the trait won't work in repos.
Here is an example Eloquent repository with all the necessary methods for scoping. These methods should go in an EloquentBaseRepository
class, to keep things DRY.
Seeding
You can manually override the scoping in your seed files to avoid difficult lookups for relations, by setting the override manually:
Note: set the tenant_id
field (or whatever name you chose for the scoping column) on your User
model to null
for any in-house staff/admins.
About
Conception: Hard fork off AuraEQ Laravel Multi Tenant. Many thanks to him for the initial work.
Packagist: globecode/laravel-multitenant
Hashtag: #laravel-multitenant
Twitter: @jhaurawachsman
Copyright (c) 2014 Jhaura Wachsman
Licensed under MIT.