Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/solutosoft/laravel-multitenant
Single database multi-tenancy solution for Laravel
https://github.com/solutosoft/laravel-multitenant
eloquent laravel laravel-package multi-tenant tenancy tenant
Last synced: 3 months ago
JSON representation
Single database multi-tenancy solution for Laravel
- Host: GitHub
- URL: https://github.com/solutosoft/laravel-multitenant
- Owner: solutosoft
- Created: 2019-02-21T13:38:11.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2024-03-20T18:23:23.000Z (11 months ago)
- Last Synced: 2024-09-28T05:15:35.560Z (4 months ago)
- Topics: eloquent, laravel, laravel-package, multi-tenant, tenancy, tenant
- Language: PHP
- Homepage:
- Size: 123 KB
- Stars: 24
- Watchers: 3
- Forks: 6
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
Awesome Lists containing this project
README
# 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://poser.pugx.org/solutosoft/laravel-multitenant/downloads.png)](https://packagist.org/packages/solutosoft/multitenant)
[![Latest Stable Version](https://poser.pugx.org/solutosoft/laravel-multitenant/v/stable.png)](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
------------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
-----1. Create table with `tenant_id` column:
```php
/**
* 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.```php
tenant_id;
}...
}class Pet extends Model
{use MultiTenant;
...
}
```
Now when you save or execute same query the `tenant_id` column will be used. Example:
```php
// It's necessary will be logged in$users = App\User::where('active', 1)->get();
// select * from `users` where `active` = 1 and tenant_id = 1$pet = Pet::create(['name' => 'Bob']);
// insert into `pet` (`name`, 'tenant_id') values ('Bob', 1)
```Auth Service Provider
---------------------It's necessary change the authentication service provider:
1. Creates new file: `app/Providers/TenantUserProvider.php`
```php
withoutGlobalScope(TenantScope::class);
}}
```2. Edit `app/Providers/AuthServiceProvider.php`
```php
[
'users' => [
'driver' => 'multitenant',
'model' => App\Models\User::class,
],
],...
```