PHPackages                             romegadigital/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. [Framework](/categories/framework)
4. /
5. romegadigital/multitenancy

AbandonedArchivedLibrary[Framework](/categories/framework)

romegadigital/multitenancy
==========================

Adds domain based multitenancy to Laravel applications.

4.0.0(2y ago)37411.5k↓33.3%291MITPHPCI failing

Since Jan 22Pushed 1y ago7 watchersCompare

[ Source](https://github.com/romegasoftware/Multitenancy)[ Packagist](https://packagist.org/packages/romegadigital/multitenancy)[ RSS](/packages/romegadigital-multitenancy/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (3)Versions (15)Used By (1)

Multitenancy Laravel Package
============================

[](#multitenancy-laravel-package)

[![Total Downloads](https://camo.githubusercontent.com/ce9c51c19f12c94ac4f168c6743637e04fb5dd103d0520a480e152778c4d289b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f726f6d6567616469676974616c2f6d756c746974656e616e63792e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/romegadigital/multitenancy)

This package provides a convenient way to add multitenancy to your Laravel application. It manages models and relationships for Tenants, identifies incoming traffic by subdomain, and associates it with a corresponding tenant. Users not linked with a specific subdomain or without a matching tenant in the Tenant table are presented with a 403 error.

**Note:** Any resources saved while accessing a scoped subdomain will automatically be saved against the current tenant, based on subdomain.

**Note:** The `admin` subdomain is reserved for the package to remove all scopes from users with a `Super Administrator` role.

Table of Contents
-----------------

[](#table-of-contents)

- [Installation](#installation)
- [Usage](#usage)
- [Console Commands](#console-commands)
- [Nova Management](#managing-with-nova)
- [Testing](#testing-package)

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

[](#installation)

#### 1. Use composer to install the package:

[](#1-use-composer-to-install-the-package)

```
composer require romegadigital/multitenancy
```

In Laravel 5.5 and newer, the service provider gets registered automatically. For older versions, add the service provider in the `config/app.php` file:

```
'providers' => [
    // ...
    RomegaDigital\Multitenancy\MultitenancyServiceProvider::class,
];
```

#### 2. Publish the config file

[](#2-publish-the-config-file)

```
php artisan vendor:publish --provider="RomegaDigital\Multitenancy\MultitenancyServiceProvider" --tag="config"
```

#### 3. Run the setup

[](#3-run-the-setup)

```
php artisan multitenancy:install
```

This command will:

- Publish and migrate required migrations
- Add a `Super Administrator` role and `access admin` permission
- Create an `admin` Tenant model

#### 4. Update your `.env` file

[](#4-update-your-env-file)

The package needs to know your base URL so it can determine what constitutes a tenant by the subdomain.

Add this to your `.env` file: `MULTITENANCY_BASE_URL=`

#### 5. Update your User model

[](#5-update-your-user-model)

Apply the `RomegaDigital\Multitenancy\Traits\HasTenants` and `Spatie\Permission\Traits\HasRoles` traits to your User model(s):

```
use Spatie\Permission\Traits\HasRoles;
use RomegaDigital\Multitenancy\Traits\HasTenants;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use HasTenants, HasRoles;
    // ...
}
```

Usage
-----

[](#usage)

Tenants require a name to identify the tenant and a subdomain that is associated with that user. Example:

`tenant1.example.com`

`tenant2.example.com`

**Note:** You define the base url `example.com` in the `config/multitenancy.php` file.

These Tenants could be added to the database like so:

```
Tenant::create([
    'name'    => 'An Identifying Name',
    'domain'  => 'tenant1'
]);
Tenant::create([
    'name'    => 'A Second Customer',
    'domain'  => 'tenant2'
]);
```

You can then attach user models to the Tenant:

```
$user = User::first();
Tenant::first()->users()->save($user);
```

Create Tenants, associate them with Users, and define access rules using provided Middleware. Check [the detailed usage guide](#detailed-usage-guide) for examples.

Detailed Usage Guide
--------------------

[](#detailed-usage-guide)

### 1. **Models and relationships:**

[](#1-models-and-relationships)

Use Eloquent to access User's tenants (`User::tenants()->get()`) and Tenant's users (`Tenant::users()->get()`). Add new tenants and their associated users to the database.

### 2. **Middleware:**

[](#2-middleware)

Add `TenantMiddleware` and `GuestTenantMiddleware` to your `app/Http/Kernel.php` file and apply them to routes.

#### Tenant Middleware

[](#tenant-middleware)

```
protected $middlewareAliases = [
    // ...
    'tenant.auth' => \RomegaDigital\Multitenancy\Middleware\TenantMiddleware::class,
];
```

Then you can bring multitenancy to your routes using middleware rules:

```
Route::group(['middleware' => ['tenant.auth']], function () {
    // ...
});
```

#### Guest Tenant Middleware

[](#guest-tenant-middleware)

This package comes with `GuestTenantMiddleware` middleware which applies the tenant scope to all models and can be used for allowing guest users to access Tenant related pages. You can add it inside your `app/Http/Kernel.php` file.

```
protected $middlewareAliases = [
    // ...
    'tenant.guest' => \RomegaDigital\Multitenancy\Middleware\GuestTenantMiddleware::class,
];
```

Then you can bring multitenancy to your routes using middleware rules:

```
Route::group(['middleware' => ['tenant.guest']], function () {
    // ...
});
```

### 3. **Tenant Assignment for Models:**

[](#3-tenant-assignment-for-models)

Make models tenant-aware by adding a trait and migration. Then apply tenant scoping automatically. This allows users to access `tenant1.example.com` and return the data from `tenant1` only.

For example, say you wanted Tenants to manage their own `Product`. In your `Product` model, add the `BelongsToTenant` trait. Then run the provided console command to add the necessary relationship column to your existing `products` table.

```
use Illuminate\Database\Eloquent\Model;
use RomegaDigital\Multitenancy\Traits\BelongsToTenant;

class Product extends Model
{
    use BelongsToTenant;

    // ...
}
```

**Add tenancy to a model's table:** `php artisan multitenancy:migration products`

### 4. **Access to Current Tenant:**

[](#4-access-to-current-tenant)

Use `app('multitenancy')->currentTenant()` to get the current tenant model.

### 5. **Admin Domain Access:**

[](#5-admin-domain-access)

Assign the `Super Administrator` role to a user to enable access to the `admin` subdomain. Manually create an admin portal if necessary.

### 6. **Auto-assign Users to Tenants:**

[](#6-auto-assign-users-to-tenants)

Enable `ignore_tenant_on_user_creation` setting to automatically assign users to the Tenant subdomain on which they are created.

### 7. **Give a user `Super Administration` rights:**

[](#7-give-a-user-super-administration-rights)

In order to access the `admin.example.com` subdomain, a user will need the `access admin` permission. This package relies on [Spatie's Laravel Permission](https://github.com/spatie/laravel-permission) package and is automatically included as a dependency when installing this package. We also provide a `Super Administrator` role on migration that has the relevant permission already associated with it. Assign the `Super Administrator` role to an admin user to provide the access they need. See the [Laravel Permission](https://github.com/spatie/laravel-permission) documentation for more on adding users to the appropriate role and permission.

The Super Administrator is a special user role with privileged access. Users with this role can access all model resources, navigate across different tenants' domains, and gain entry to the `admin` subdomain where all tenant scopes are disabled.

When a user is granted the `Super Administrator` role, they can freely access the `admin` subdomain. In this context, tenant scopes aren't applied. This privilege allows Super Administrators to manage data across all instances without requiring specific access to each individual tenant's account.

Give a user `Super Administration` rights: `php artisan multitenancy:super-admin admin@example.com`

Managing with Nova
------------------

[](#managing-with-nova)

You can manage the resources of this package in Nova with the [MultitenancyNovaTool](https://github.com/romegadigital/MultitenancyNovaTool).

Testing Package
---------------

[](#testing-package)

Run tests with the command:

`php vendor/bin/testbench package:test`

###  Health Score

45

—

FairBetter than 92% of packages

Maintenance29

Infrequent updates — may be unmaintained

Popularity44

Moderate usage in the ecosystem

Community26

Small or concentrated contributor base

Maturity68

Established project with proven stability

 Bus Factor2

2 contributors hold 50%+ of commits

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

Recently: every ~278 days

Total

14

Last Release

1075d ago

Major Versions

1.0.2 → 2.0.02019-02-27

2.1.4 → 3.0.02022-06-07

3.0.1 → 4.0.02023-05-31

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/219298?v=4)[Braden Keith](/maintainers/bradenkeith)[@bradenkeith](https://github.com/bradenkeith)

---

Top Contributors

[![bradenkeith](https://avatars.githubusercontent.com/u/219298?v=4)](https://github.com/bradenkeith "bradenkeith (53 commits)")[![Naoray](https://avatars.githubusercontent.com/u/10154100?v=4)](https://github.com/Naoray "Naoray (41 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (6 commits)")[![madurapa](https://avatars.githubusercontent.com/u/4289578?v=4)](https://github.com/madurapa "madurapa (3 commits)")[![kylebarney](https://avatars.githubusercontent.com/u/15039520?v=4)](https://github.com/kylebarney "kylebarney (2 commits)")[![jumbaeric](https://avatars.githubusercontent.com/u/7954516?v=4)](https://github.com/jumbaeric "jumbaeric (1 commits)")[![joriskuijpers](https://avatars.githubusercontent.com/u/7311629?v=4)](https://github.com/joriskuijpers "joriskuijpers (1 commits)")[![ConsoleTVs](https://avatars.githubusercontent.com/u/6124435?v=4)](https://github.com/ConsoleTVs "ConsoleTVs (1 commits)")[![tomhatzer](https://avatars.githubusercontent.com/u/3952168?v=4)](https://github.com/tomhatzer "tomhatzer (1 commits)")[![wojciechgabrys](https://avatars.githubusercontent.com/u/153263?v=4)](https://github.com/wojciechgabrys "wojciechgabrys (1 commits)")

### Embed Badge

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

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

###  Alternatives

[apiato/apiato

A flawless framework for building scalable and testable API-Centric Apps with PHP and Laravel.

3.1k31.8k](/packages/apiato-apiato)[nasirkhan/laravel-starter

A CMS like modular Laravel starter project.

1.4k2.7k](/packages/nasirkhan-laravel-starter)[daniel-cintra/modular

A fast way to develop web apps using Laravel, Vue and Inertia.

1763.8k1](/packages/daniel-cintra-modular)[fleetbase/core-api

Core Framework and Resources for Fleetbase API

1225.0k10](/packages/fleetbase-core-api)

PHPackages © 2026

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