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

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

solutosoft/laravel-multitenant
==============================

The multi tenant control with shared table

1.0.6(2y ago)25605↓100%6[1 PRs](https://github.com/solutosoft/laravel-multitenant/pulls)BSD-3-ClausePHP

Since Feb 20Pushed 2y ago2 watchersCompare

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

READMEChangelog (7)Dependencies (4)Versions (9)Used By (0)

Laravel Multi Tenancy
=====================

[](#laravel-multi-tenancy)

[![Build Status](https://github.com/solutosoft/laravel-multitenant/actions/workflows/tests.yml/badge.svg)](https://github.com/solutosoft/laravel-multitenant/actions)[![Total Downloads](https://camo.githubusercontent.com/d8617a9dd8c22fdd94cbbd8f539ed7972ebc762e97b8a066b64652fe902dd3ce/68747470733a2f2f706f7365722e707567782e6f72672f736f6c75746f736f66742f6c61726176656c2d6d756c746974656e616e742f646f776e6c6f6164732e706e67)](https://packagist.org/packages/solutosoft/multitenant)[![Latest Stable Version](https://camo.githubusercontent.com/e1c373932efacbf998a852a2bba4e3a174fea3149156fc3216efcdf3ddfafc44/68747470733a2f2f706f7365722e707567782e6f72672f736f6c75746f736f66742f6c61726176656c2d6d756c746974656e616e742f762f737461626c652e706e67)](https://packagist.org/packages/solutosoft/multitenant)

The **Shared Database** used by all tenants, means that we keep data from all the tenants in the same database. To isolate tenant specific data, we will have to add a discriminator column like `tenant_id` to every table which is tenant specific, and to make sure that all the queries and commands will filter the data based on it.

With this strategy dealing with tenant shared data is simple, we just don't filter it. Isolating data is what we need to deal with. For this we need to make sure that ALL the queries and the commands that deal with tenant specific data get filtered by the `tenant_id`.

This extension allows control the Eloquent with shared database used by all tenants.

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

[](#installation)

The preferred way to install this library is through composer.

Either run

`composer require --prefer-dist solutosoft/laravel-multitenant "*"`

or add

`"solutosoft/laravel-multitenant": "*"`

to the require section of your composer.json.

Usage
-----

[](#usage)

1. Create table with `tenant_id` column:

```
/**
 * Run the migrations.
 *
 * @return void
  */
public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('firstName');
        $table->string('lastName');
        $table->string('login')->nullable();
        $table->string('password')->nullable();
        $table->string('remember_token')->nullable();
        $table->string('active');
        $table->integer('tenant_id')->nullable(true);
    });

    $Schema::create('pets', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->integer('tenant_id');
    });
}
```

2. Uses the `MultiTenant` trait, it add a [Global Scope](https://laravel.com/docs/5.7/eloquent#global-scope) filtering any query by `tenant_id` column.

```
