PHPackages                             rndwiga/laravel-multi-db-tenant - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. rndwiga/laravel-multi-db-tenant

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

rndwiga/laravel-multi-db-tenant
===============================

This package is used to create and manage laravel multitenant applications

1.0.0(9y ago)014MITPHPPHP &gt;=5.5.9

Since Feb 29Pushed 9y ago1 watchersCompare

[ Source](https://github.com/Rndwiga/laravel-multi-db-tenant)[ Packagist](https://packagist.org/packages/rndwiga/laravel-multi-db-tenant)[ RSS](/packages/rndwiga-laravel-multi-db-tenant/feed)WikiDiscussions master Synced 2mo ago

READMEChangelog (1)Dependencies (8)Versions (4)Used By (0)

[![Build Status](https://camo.githubusercontent.com/f8a9a3957491311f5e031ce82e8082dc75c94e84ac652cd8dba89f646cef1feb/68747470733a2f2f7472617669732d63692e6f72672f67616d657277616c742f6c6172616d756c7469646274656e616e742e7376673f6272616e63683d6d6173746572)](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 rndwiga\MultiTenant

```

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

### Providers

[](#providers)

```
rndwiga\MultiTenant\LaraMultiDbTenantServiceProvider::class

```

### Facade

[](#facade)

```
'LaraMultiDbTenant' => rndwiga\MultiTenant\Facade::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 rndwiga\MultiTenant\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);

```

Questions
=========

[](#questions)

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

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
5. Command to copy any needed data from template database to all tenant databases
6. Add tests

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity60

Established project with proven stability

 Bus Factor1

Top contributor holds 81.6% 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 ~169 days

Total

3

Last Release

3387d ago

### Community

Maintainers

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

---

Top Contributors

[![gamerwalt](https://avatars.githubusercontent.com/u/4555554?v=4)](https://github.com/gamerwalt "gamerwalt (31 commits)")[![Rndwiga](https://avatars.githubusercontent.com/u/4418418?v=4)](https://github.com/Rndwiga "Rndwiga (5 commits)")[![mtdavidson](https://avatars.githubusercontent.com/u/201792?v=4)](https://github.com/mtdavidson "mtdavidson (1 commits)")[![rikochet](https://avatars.githubusercontent.com/u/1213193?v=4)](https://github.com/rikochet "rikochet (1 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/rndwiga-laravel-multi-db-tenant/health.svg)

```
[![Health](https://phpackages.com/badges/rndwiga-laravel-multi-db-tenant/health.svg)](https://phpackages.com/packages/rndwiga-laravel-multi-db-tenant)
```

###  Alternatives

[aedart/athenaeum

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

255.2k](/packages/aedart-athenaeum)[barryvdh/laravel-ide-helper

Laravel IDE Helper, generates correct PHPDocs for all Facade classes, to improve auto-completion.

14.9k123.0M687](/packages/barryvdh-laravel-ide-helper)[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)[flarum/core

Delightfully simple forum software.

211.3M1.9k](/packages/flarum-core)[orchestra/canvas

Code Generators for Laravel Applications and Packages

21017.2M158](/packages/orchestra-canvas)

PHPackages © 2026

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