PHPackages                             digitools/laravel-multi-tenancy - 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. digitools/laravel-multi-tenancy

ActiveLibrary[Framework](/categories/framework)

digitools/laravel-multi-tenancy
===============================

A Laravel Multi-Tenancy Toolkit for SaaS applications

02PHP

Since Dec 6Pushed 1y agoCompare

[ Source](https://github.com/digimantra/laravel-multi-tenancy)[ Packagist](https://packagist.org/packages/digitools/laravel-multi-tenancy)[ RSS](/packages/digitools-laravel-multi-tenancy/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

Laravel Multi-Tenancy
=====================

[](#laravel-multi-tenancy)

A simple and lightweight multi-tenancy solution for Laravel applications, supporting tenant database switching, tenant migrations, and helper functions.

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

[](#installation)

### Step 1: Install the Package

[](#step-1-install-the-package)

Install the package via Composer:

```
composer require digitools/multi-tenancy
```

### Step 2: Publish Configuration and Migrations

[](#step-2-publish-configuration-and-migrations)

Publish the configuration file and migrations:

```
php artisan vendor:publish --provider="Digitools\MultiTenancy\Providers\MultiTenancyServiceProvider" --tag=multi-tenancy
```

This will create:

- A configuration file: `config/multi-tenancy.php`.
- A migration file: `database/migrations/{timestamp}_create_tenants_table.php`.

### Step 3: Run the Migration

[](#step-3-run-the-migration)

Run the migration to create the `tenants` table:

```
php artisan migrate
```

### Step 4: Add Database Connections

[](#step-4-add-database-connections)

Update your `config/database.php` file to include a `tenant` database connection:

```
'connections' => [
    'tenant' => [
        'driver' => 'mysql',
        'host' => env('DB_HOST', '127.0.0.1'),
        'port' => env('DB_PORT', '3306'),
        'database' => null, // dynamically set by the package
        'username' => env('DB_USERNAME', 'root'),
        'password' => env('DB_PASSWORD', ''),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'strict' => true,
    ],
    // other connections...
],
```

---

Usage
-----

[](#usage)

### Create Tenants

[](#create-tenants)

Add entries to the `tenants` table. Each tenant should have:

- A `name`.
- A unique `domain`.
- A unique `database`.

Example:

```
use Digitools\MultiTenancy\Models\Tenant;

// Create a tenant
Tenant::create([
    'name' => 'Tenant 1',
    'domain' => 'tenant1.example.com',
    'database' => 'tenant1_db',
]);

Tenant::create([
    'name' => 'Tenant 2',
    'domain' => 'tenant2.example.com',
    'database' => 'tenant2_db',
]);
```

### Middleware for Tenant Resolution

[](#middleware-for-tenant-resolution)

Create a middleware to resolve the current tenant based on the domain:

```
namespace App\Http\Middleware;

use Closure;
use Digitools\MultiTenancy\Models\Tenant;
use Digitools\MultiTenancy\Services\DatabaseSwitcher;

class ResolveTenant
{
    public function handle($request, Closure $next)
    {
        $host = $request->getHost();
        $tenant = Tenant::where('domain', $host)->first();

        if ($tenant) {
            app(DatabaseSwitcher::class)->switchToTenant($tenant->database);
            app()->instance('current_tenant', $tenant);
        }

        return $next($request);
    }
}
```

Register the middleware in `app/Http/Kernel.php`:

```
protected $middlewareGroups = [
    'web' => [
        // other middleware...
        \App\Http\Middleware\ResolveTenant::class,
    ],
];
```

### Run Tenant-Specific Migrations

[](#run-tenant-specific-migrations)

To migrate tenant-specific tables, use:

```
php artisan tenant:migrate --tenant_id=1
```

Or migrate all tenants:

```
php artisan tenant:migrate --all
```

Ensure tenant-specific migrations are placed under `database/migrations/tenant`.

---

Example Testing
---------------

[](#example-testing)

### Seed Data

[](#seed-data)

Seed tenant databases with some sample data:

```
$tenant = Tenant::first();
app(Digitools\MultiTenancy\Services\DatabaseSwitcher::class)->switchToTenant($tenant->database);

DB::table('users')->insert([
    'name' => 'Tenant User',
    'email' => 'user@tenant.com',
    'password' => bcrypt('password'),
]);
```

### Test Tenant Switching

[](#test-tenant-switching)

Test API routes or controllers with tenant-specific data:

```
use Illuminate\Support\Facades\Route;

Route::get('/test', function () {
    $tenant = current_tenant();
    return response()->json([
        'tenant' => $tenant->name,
        'database' => DB::connection()->getDatabaseName(),
    ]);
});
```

Access this route for each tenant domain to verify the correct tenant and database are being resolved.

---

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

[](#configuration)

Customize the `multi-tenancy.php` configuration file as needed. Example options include default database, tenant identification strategy, and more.

---

Contributing
------------

[](#contributing)

Feel free to open issues or submit pull requests for enhancements or bug fixes.

---

License
-------

[](#license)

This package is open-sourced software licensed under the [MIT license](https://opensource.org/licenses/MIT).

###  Health Score

14

—

LowBetter than 2% of packages

Maintenance31

Infrequent updates — may be unmaintained

Popularity2

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity16

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% 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.

### Community

Maintainers

![](https://www.gravatar.com/avatar/b0915329748e0b3b6062e1a0110cd94552cd90313a133a087510813a6734fb92?d=identicon)[hs-dml](/maintainers/hs-dml)

---

Top Contributors

[![rishab-php-digi](https://avatars.githubusercontent.com/u/91057086?v=4)](https://github.com/rishab-php-digi "rishab-php-digi (2 commits)")

### Embed Badge

![Health badge](/badges/digitools-laravel-multi-tenancy/health.svg)

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

###  Alternatives

[laravel/telescope

An elegant debug assistant for the Laravel framework.

5.2k67.8M192](/packages/laravel-telescope)[spiral/roadrunner

RoadRunner: High-performance PHP application server and process manager written in Go and powered with plugins

8.4k12.2M84](/packages/spiral-roadrunner)[nolimits4web/swiper

Most modern mobile touch slider and framework with hardware accelerated transitions

41.8k177.2k1](/packages/nolimits4web-swiper)[laravel/dusk

Laravel Dusk provides simple end-to-end testing and browser automation.

1.9k36.7M259](/packages/laravel-dusk)[laravel/prompts

Add beautiful and user-friendly forms to your command-line applications.

708181.8M596](/packages/laravel-prompts)[cakephp/chronos

A simple API extension for DateTime.

1.4k47.7M121](/packages/cakephp-chronos)

PHPackages © 2026

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