PHPackages                             gamerwalt/laramultidbtenant - 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. gamerwalt/laramultidbtenant

ActiveLibrary

gamerwalt/laramultidbtenant
===========================

This package is used to create and manage laravel multitenant applications that also creates and migrates multiple databases per tenant

2.1.x-dev(6y ago)141.2k8[2 issues](https://github.com/gamerwalt/laramultidbtenant/issues)MITPHPPHP &gt;=7.2CI failing

Since Feb 29Pushed 6y ago3 watchersCompare

[ Source](https://github.com/gamerwalt/laramultidbtenant)[ Packagist](https://packagist.org/packages/gamerwalt/laramultidbtenant)[ RSS](/packages/gamerwalt-laramultidbtenant/feed)WikiDiscussions 2.1.5 Synced 2mo ago

READMEChangelogDependencies (8)Versions (13)Used By (0)

[![Build Status](https://camo.githubusercontent.com/77f84ccf04865722036d79d8a25bae6b5ee0d21e703fb4473c20d1c9590d2716/68747470733a2f2f7472617669732d63692e6f72672f67616d657277616c742f6c6172616d756c7469646274656e616e742e7376673f6272616e63683d646576656c6f70)](https://travis-ci.org/gamerwalt/laramultidbtenant)

Multi-Database Tenant Package for Laravel
=========================================

[](#multi-database-tenant-package-for-laravel)

This package is meant for Laravel applications to help with Multi-Database Tenant Applications.

A sample laravel application that uses this package can be found at [github](https://github.com/gamerwalt/TodoMultitenantDemo)

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

[](#installation)

```
composer require gamerwalt/laramultidbtenant

```

Add the service providers to your application providers array in config/app.php A Facade is also available and can be added...

### Providers

[](#providers)

```
gamerwalt\LaraMultiDbTenant\LaraMultiDbTenantServiceProvider::class

```

### Facade

[](#facade)

```
'LaraMultiTenantDB' => gamerwalt\LaraMultiDbTenant\LaraMultiTenantDB::class

```

### Configuration Files

[](#configuration-files)

```
php artisan vendor:publish

```

This will copy the laramultidbtenant.php configuration file into the app/config directory as well as copy the tenant, tenant\_users and tenant\_databases migration files needed. Create your models.

Setup the database for your Tenants in your .env files

### Create the needed model classes

[](#create-the-needed-model-classes)

This will create the Tenant, TenantDatabase, TenantUser models. Make sure to provide the right relationships and don't forget to create the relationships within the [User, Tenant, TenantUser and TenantDatabase models](https://github.com/gamerwalt/TodoMultitenantDemo/tree/master/app) as well as set the connection properties.

```
php artisan tenant:basemodels

```

```
class Tenant extends Model
{
    protected $connection = 'todo';

    protected $table = 'tenants';

    protected $primaryKey = 'tenant_id';

    protected $fillable = ['tenant_uid', 'company_name', 'short_company_name', 'database_prefix', 'address'];
    .......

```

### Create the template, tenant migration folders as well as a tenant public folder

[](#create-the-template-tenant-migration-folders-as-well-as-a-tenant-public-folder)

```
php artisan tenant:folders

```

This package registers a middleware called authTenant within it's service provider. This is needed in order to automatically set the default connections for the Application's model

### For a todo application [see demo application](https://github.com/gamerwalt/TodoMultitenantDemo)

[](#for-a-todo-application-see-demo-application)

Inside your controllers that will control tenant specific routing, add the following to the \_\_construct methods

```
$this->middleware('auth');
$this->middleware('authTenant'); //This is automatically pushed to laravel's kernel

```

also make sure to include the trait to do migrations automatically once the tenant has registered their company...

```
use MigrateTenantDatabase;

```

Complete namespace for this is...

```
use gamerwalt\LaraMultiDbTenant\Traits\MigrateTenantDatabase;

```

### Create your migration files

[](#create-your-migration-files)

This assumes you have already done...

```
php artisan vendor:publish

```

So create your migration files...

```
php artisan make:migration create_todo_table --create=todos --path=database/migrations/tenant

```

#### Your TenantDatabase Model

[](#your-tenantdatabase-model)

**This is important**

Your TenantDatabase model **must** implement the ITenantDatabase interface

```
class TenantDatabase extends Model implements ITenantDatabase

```

Once that's done, start with registering a user, at least collect information needed. In your controller that registers the user, tenant, tenant\_database, tenant\_users you should be able to do...

```
$this->migrateTenantDatabase($tenantDatabase->hostname, $tenantDatabase->database_name, $tenantDatabase->user_name, $tenantDatabase->password);

```

Use [this](https://github.com/gamerwalt/TodoMultitenantDemo) demo as a guide. See SessionsController@postRegister. This should show the code...

```
    $input = $request->all();
    $email = $input['email'];
    $password = $input['password'];
    $name = $input['name'];
    $companyName = $input['company_name'];

    DB::beginTransaction();

    $user = $this->registerUser($email, $password, $name);
    $tenant = $this->registerTenant($companyName);
    $tenantUser = $this->createTenantUserDetails($user, $tenant);
    $tenantDatabase = $this->createTenantDatabase($tenant);

    DB::commit();
    //now we can migrate the database of the tenant using the tenant database settings

    $this->migrateTenantDatabase($tenantDatabase->hostname, $tenantDatabase->database_name,
                                     $tenantDatabase->user_name, $tenantDatabase->password);

```

### Resyncing and Updating Tables using migrations...

[](#resyncing-and-updating-tables-using-migrations)

**Create new migration files for update**

php artisan make:migration add\_column\_to\_todo\_table --table=todos --path=database/migrations/tenant

Once that's done, run the following commands

php artisan tenant:migrate-resync

### Multiple Tenants Per User

[](#multiple-tenants-per-user)

This package allows a user to have multiple tenants/companies per user. This means that if a user is registered or added to 2 tenants, the user will be able to select which tenant/company they want to logon to.

Questions
=========

[](#questions)

Twitter: [@gamerwalt](https://twitter.com/gamerwalt)

Future
======

[](#future)

1. Do some clean up of the code.
2. Command for creating template and tenant migration files
3. Command to migrate to template database
4. Command to sync new migration files (Done)
5. Command to copy any needed data from template database to all tenant databases
6. Add tests

###  Health Score

29

—

LowBetter than 60% of packages

Maintenance17

Infrequent updates — may be unmaintained

Popularity25

Limited adoption so far

Community15

Small or concentrated contributor base

Maturity51

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 97.9% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~148 days

Recently: every ~1 days

Total

9

Last Release

2538d ago

Major Versions

1.1.x-dev → 2.0.x-dev2019-05-19

PHP version history (2 changes)1.0.x-devPHP &gt;=5.5.9

2.0.x-devPHP &gt;=7.2

### Community

Maintainers

![](https://www.gravatar.com/avatar/e8c86eeb46924eaf2154334fa1e5e8284cd613cf1a670670c10e449e248208ff?d=identicon)[gamerwalt](/maintainers/gamerwalt)

---

Top Contributors

[![gamerwalt](https://avatars.githubusercontent.com/u/4555554?v=4)](https://github.com/gamerwalt "gamerwalt (93 commits)")[![mtdavidson](https://avatars.githubusercontent.com/u/201792?v=4)](https://github.com/mtdavidson "mtdavidson (1 commits)")[![williamsduarte](https://avatars.githubusercontent.com/u/54290322?v=4)](https://github.com/williamsduarte "williamsduarte (1 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/gamerwalt-laramultidbtenant/health.svg)

```
[![Health](https://phpackages.com/badges/gamerwalt-laramultidbtenant/health.svg)](https://phpackages.com/packages/gamerwalt-laramultidbtenant)
```

###  Alternatives

[roots/acorn

Framework for Roots WordPress projects built with Laravel components.

9682.1M97](/packages/roots-acorn)[laravel/pulse

Laravel Pulse is a real-time application performance monitoring tool and dashboard for your Laravel application.

1.7k12.1M99](/packages/laravel-pulse)[aedart/athenaeum

Athenaeum is a mono repository; a collection of various PHP packages

245.2k](/packages/aedart-athenaeum)[laravel/passport

Laravel Passport provides OAuth2 server support to Laravel.

3.4k85.0M532](/packages/laravel-passport)[genealabs/laravel-model-caching

Automatic caching for Eloquent models.

2.4k4.8M26](/packages/genealabs-laravel-model-caching)[tucker-eric/eloquentfilter

An Eloquent way to filter Eloquent Models

1.8k4.8M26](/packages/tucker-eric-eloquentfilter)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
