PHPackages                             globecode/laravel-multitenant - 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. globecode/laravel-multitenant

ActiveLibrary

globecode/laravel-multitenant
=============================

Laravel Multi-Tenant

v0.2.0(11y ago)203362[1 PRs](https://github.com/globecode/laravel-multitenant/pulls)MITPHPPHP &gt;=5.4.0

Since Feb 2Pushed 11y ago3 watchersCompare

[ Source](https://github.com/globecode/laravel-multitenant)[ Packagist](https://packagist.org/packages/globecode/laravel-multitenant)[ RSS](/packages/globecode-laravel-multitenant/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (1)Versions (3)Used By (0)

Laravel Multi-Tenant
====================

[](#laravel-multi-tenant)

Sep 29, 2014: WIP for release as an L4 package.

Installation
============

[](#installation)

1. Require this package in your `composer.json` file in the `"require"` block:

    ```
    "globecode/laravel-multitenant": "dev-master"
    ```
2. Add the service provider to the providers array in `app/config/app.php`. This is only used for the "Publish `config`" command two steps down:

    ```
    'GlobeCode\LaravelMultiTenant\LaravelMultiTenantServiceProvider'
    ```
3. `cd` into your project directory and update via *Composer*:

    ```
    composer update
    ```
4. Publish the `config` to your application. This allows you to change the name of the *Tenant* column name in your schema. A `config` file will be installed to `app/config/packages/globecode/laravel-multitenant/` which you can edit:

    ```
    php artisan config:publish globecode/laravel-multitenant
    ```
5. Create and run new *migrations* to setup the necessary schema. Example migrations are provided in the Package `migrations` folder.
6. Add the `getTenantId()` method to your `User` (and any other "scoped" models) or just once in a `BaseModel` (recommended):

    ```
    /**
     * Get the value to scope the "tenant id" with.
     *
     * @return string
     */
    public function getTenantId()
    {
        return (isset($this->tenant_id)) ? $this->tenant_id : null;
    }
    ```
7. Optional **Global Override**. If you want to globally override scope, such as for an *Admin*, then add the `isAdmin()` method to your `User` model. The `ScopedByTenant` trait will look for this method and automatically override the scope on all queries if this method returns `true`:

    ```
    /**
     * Does the current user have an 'admin' role?
     *
     * @return bool
     */
    public function isAdmin()
    {
        // Change to return true using whatever
        // roles/permissions you use in your app.
        return $this->hasRole('admin');
    }
    ```

Usage
=====

[](#usage)

1. 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!

    ```
