Automatic multi-tenancy for your Laravel app.

You won't have to change a thing in your application's code.

  • No model traits to change database connection
  • No replacing of Laravel classes (Cache, Storage, ...) with tenancy-aware classes
  • Built-in tenant identification based on hostname (including second level domains)
Star Laravel 5.8 Latest Stable Version Travis CI build codecov

Multi-tenancy is something that can be handled in the background. Why should your app's code be polluted with tons of tenancy features implementations?

stancl/tenancy is a Laravel package that implements multi-database tenancy into your application in a way that doesn't require you to change anything in your existing codebase.

How does it work?

  1. The tenant is identified using the current hostname.
  2. Database & Redis connections are switched, cache is tagged and filesystem root paths are suffixed.
  3. Your code is automatically multi-tenant. No changes needed.

Easy Installation

Just run composer require and move your tenant routes.

$ composer require stancl/tenancy
$ move routes/web.php routes/tenant.php
$ touch routes/web.php
cache()->remember('total_revenue', function () {
   return Order::with('products')->get()->products->sum();
});

Automatic Data Separation

Tenants' databases, Redis stores, filesystems and caches are automatically separated. You can write your app like you're used to and let the package worry about tenant data separation.

Configurable

Even though everything happens automatically, you're still in control. You can configure how database names, Redis keys and filesystem paths are prefixed, as well as how cache is tagged and what DB/Redis connections and FS disks are made multi-tenant.

'redis' => [
  'tenancy' => false,
  'prefix_base' => 'tenant',
  'prefixed_connections' => [
    'default',
    'cache',
  ],
]
$ artisan tenants:seed --tenants=8075a580-1cb8-11e9-8822-49c5d8f8ff23
Tenant: 8075a580-1cb8-11e9-8822-49c5d8f8ff23 (laravel.localhost)
Database seeding completed successfully.

CLI-friendly

stancl/tenancy comes with artisan commands for migrating, rolling back, as well as seeding tenant databases.

SOLID Extensibility

The package follows the SOLID principles and is open to extension. You're free to write (and contribute! ) your own storage drivers and database managers.

class PostgreSQLDatabaseManager implements TenantDatabaseManager
{
  public function createDatabase($name)
  {
    //
  }

Stay Updated

If you'd like to be notified about new versions and related stuff, you can sign up for e-mail notificatons or join our Telegram channel.

Subscribe to e-mail notifications

What would you like to be notified about?