PHPackages                             ollieread/laravel-multitenancy - 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. ollieread/laravel-multitenancy

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

ollieread/laravel-multitenancy
==============================

2.2.4(9y ago)211473MITPHPPHP &gt;=5.6.4

Since Jan 21Pushed 8y ago1 watchersCompare

[ Source](https://github.com/ollieread/multitenancy)[ Packagist](https://packagist.org/packages/ollieread/laravel-multitenancy)[ RSS](/packages/ollieread-laravel-multitenancy/feed)WikiDiscussions master Synced today

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

Laravel Multitenancy
====================

[](#laravel-multitenancy)

[![Latest Stable Version](https://camo.githubusercontent.com/677ca5aab08510dc6d66dccbb1603fa0967e1e2674ce0f8d9b9af8795d9da131/68747470733a2f2f706f7365722e707567782e6f72672f6f6c6c6965726561642f6c61726176656c2d6d756c746974656e616e63792f762f737461626c652e706e67)](https://packagist.org/packages/ollieread/laravel-multitenancy) [![Total Downloads](https://camo.githubusercontent.com/087c468a5d44f75fa179c76292fe54b8698bb698b103c1e8b013d8db4883c2d4/68747470733a2f2f706f7365722e707567782e6f72672f6f6c6c6965726561642f6c61726176656c2d6d756c746974656e616e63792f646f776e6c6f6164732e706e67)](https://packagist.org/packages/ollieread/laravel-multitenancy) [![Latest Unstable Version](https://camo.githubusercontent.com/865e0b19c68983c70d4f2b2d0c0c65e55cc807d6022891d9e0298199d21eaba9/68747470733a2f2f706f7365722e707567782e6f72672f6f6c6c6965726561642f6c61726176656c2d6d756c746974656e616e63792f762f756e737461626c652e706e67)](https://packagist.org/packages/ollieread/laravel-multitenancy) [![License](https://camo.githubusercontent.com/20780cf0245037dda30b4c441c184cad51c67447dea17eab17bd0d39784a69af/68747470733a2f2f706f7365722e707567782e6f72672f6f6c6c6965726561642f6c61726176656c2d6d756c746974656e616e63792f6c6963656e73652e706e67)](https://packagist.org/packages/ollieread/laravel-multitenancy)

- **Laravel**: 5.3 &amp; 5.4
- **Author**: Ollie Read
- **Author Homepage**:

Laravel package for multitenancy using subdomain and/or domain based identification. The package itself works much in the same way as the default Auth library.

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

[](#installation)

Firstly you want to include this package in your composer.json file.

```
"require": {
    "ollieread/laravel-multitenancy": "^2"
}

```

Now you'll want to update or install via composer.

```
composer update

```

Next you open up app/config/app.php and add the following.

```
Ollieread\Multitenancy\ServiceProvider::class,

```

Then the facade.

```
'Multitenancy' => Ollieread\Multitenancy\Facades\Multitenancy::class,

```

Finally, run the following command to publish the config.

```
php artisan vendor:publish --provider=Ollieread\Multitenancy\ServiceProvider

```

Configuration
-------------

[](#configuration)

There are three main parts to the configuration.

\#####The provider

```
'provider'      => 'eloquent',

```

By default this is can be either `eloquent` or `database`. If you add a custom provider, you'd use the name here.

\#####The domain

```
'domain'        => env('MULTITENANCY_DOMAIN', 'mydomain.com'),

```

The domain that should be used for tenant based subdomains.

\#####The Provider settings

```
'eloquent'      => [
    // The model representing a tenant
    'model'         => Ollieread\Multitenancy\Models\Tenant::class
],

'database'      => [
    // The table where tenants are stored
    'table'         => 'tenants',
    // The foreign key for identifying tenant ownership
    'foreign_key'   => 'tenant_id',
    // The identifiers used to identify a tenant
    'identifiers'   => [
        'slug', 'domain'
    ]
]

```

These particular settings are defined by the individual providers and the defaults contain small descriptions.

Current Tenant
--------------

[](#current-tenant)

Access the currently identified tenant by using `Multitenancy::tenant()`.

Tenant Routes
-------------

[](#tenant-routes)

There is a method for create route groups that should be part of the tenant system.

```
Multitenancy::routes(function (Router $router) {
    $router->get('/tenancy', function() {
        $tenant = Multitenancy::tenant();
        dd($tenant);
    });
});

```

It is worth noting that if this method is called inside a group that has the default `web` group, it'll error. You should instead wrap the `web` group routes in this, eg:

```
protected function mapWebRoutes()
{
    Multitenancy::routes(function (Router $router) {
        Route::middleware('web')
            ->namespace($this->namespace)
            ->group(base_path('routes/web.php'));
    });
}

```

If you wish to use your own route groups, just make sure you load the following middleware before any authentication related middleware.

```
Ollieread\Multitenancy\Middleware\LoadTenant

```

To generate a url for a tenant based route, you can use the following methods:

```
Multitenancy::route($name, $paramaters = [], $absolute = false);
Multitenancy::url($path, $paramaters = [], $secure = false);

```

These methods act the same as `route()` and `url()` except that they automatically add the correct domain for the current tenant. NOTE: The `url()` method has been removed for now as it wasn't working exactly as intended.

Eloquent
--------

[](#eloquent)

If using the eloquent provider, the specified model must implement:

```
Ollieread\Multitenancy\Contracts\Tenant

```

There is a trait available that provides implementation using the default column names `slug` and `domain`. It also offers access to `route()` and `url()` on the model. This trait is:

```
Ollieread\Multitenancy\Traits\Tenant

```

### Scopes

[](#scopes)

There is a scope available to you for use on models that belong to a tenant. To use this, add the following trait to the models you wish to belong to a tenant:

```
Ollieread\Multitenancy\Traits\TenantOwned

```

This only works for models that have the tenant foreign key as a column, and is designed to prevent you from having to manually add where clauses everywhere.

If you wish to see all entries regardless of the current tenant, use the `withAll()` method.

Multi Database
--------------

[](#multi-database)

If you wish to use a multi database multitenant approach (each tenant has their own database), you can do so with this package.

### Configuring

[](#configuring)

Create yourself a base configuration within the database configuration file. For example, duplicate the mysql connection information, rename to whatever you would like, set the database name to be an empty string, and then update the `multidatabase.connection` setting in the multitenancy configuration file.

By default, the connection will look for a database named `tenant_{id}` where `{id}` is the id of the row from the tenant model. To override this, you need to provide a configuration parser, which will allow you to adjust all of the connection configuration settings per tenant. Below is how to do so, using the default implementation.

```
Multitenancy::setConnectionParser(function ($config = [], Tenant $tenant) {
    $config['database'] = 'tenant_'.$tenant->id;
    return $config;
});

```

### Implementing

[](#implementing)

To use this, simple reference the `multitenancy` connection either in the Eloquent model property, or by using `DB::connection('multitenancy')` when using the base database library.

Authentication
--------------

[](#authentication)

If you're using the `TenantOwned` trait on your user model you won't need to do anything with the Eloquent provider.

For session based authentication you'll want to use the `session.multi` guard, which is identical to the default session guard, except that prefixes session and cookie names with the tenant primary identifier to allow users to be logged into multiple tenants at once.

If you're using the Database provider for auth, you'll want to use the `database.multi` provider so that you only retrieve records specific to the current tenant.

Custom Providers
----------------

[](#custom-providers)

This package supports custom providers. To create a custom provider, create a class that implements `Ollieread\Multitenancy\Contracts\Provider` and then register it like so:

```
Multitenancy::extend('eloquent', function ($app, $config) {
    return new Eloquent($config['model']);
})->extend('database', function ($app, $config) {
    return new Database($app['db']->connection(), $config['table'], $config['identifiers']);
});

```

The method is setup for daisy chaining should you need to add multiple.

###  Health Score

31

—

LowBetter than 66% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity20

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity65

Established project with proven stability

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 ~42 days

Recently: every ~109 days

Total

12

Last Release

2988d ago

Major Versions

v1.0.1 → v2.0.02017-01-28

2.2.4 → v3.x-dev2018-04-25

PHP version history (2 changes)v1.0.0PHP &gt;=5.6.4

v3.x-devPHP ^7.1

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/469515?v=4)[Ollie Read](/maintainers/ollieread)[@ollieread](https://github.com/ollieread)

---

Tags

laravelmultidatabasemultitenancytenant

### Embed Badge

![Health badge](/badges/ollieread-laravel-multitenancy/health.svg)

```
[![Health](https://phpackages.com/badges/ollieread-laravel-multitenancy/health.svg)](https://phpackages.com/packages/ollieread-laravel-multitenancy)
```

###  Alternatives

[anourvalar/eloquent-serialize

Laravel Query Builder (Eloquent) serialization

11222.5M33](/packages/anourvalar-eloquent-serialize)[statamic-rad-pack/runway

Eloquently manage your database models in Statamic.

135212.4k7](/packages/statamic-rad-pack-runway)[mozex/laravel-scout-bulk-actions

Import, flush, and queue-import all your Laravel Scout searchable models at once. Auto-discovers models, runs in bulk, tracks progress.

1437.7k](/packages/mozex-laravel-scout-bulk-actions)[ramadan/easy-model

A Laravel package for enjoyably managing database queries.

111.6k](/packages/ramadan-easy-model)

PHPackages © 2026

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