Automatic Tenancy For Your Laravel App

Automatic Tenancy For Your Laravel App

The stancl/tenancy package provides automatic multi-tenancy for your Laravel application:

  • No model traits to change database connections
  • No replacing Laravel classes with tenancy-aware classes
  • Built-in tenant identification based on hostname, including second-level domains)

It works by using the current hostname as the tenant, switching database and Redis connections, and suffixing things like cache and filesystems.

The stancl/tenancy package comes with artisan commands and an API to do things like creating new tenants:

use Stancl\Tenancy\Tenant;

    ->withDomains(['', ''])
    ->withData(['plan' => 'free'])

This package also provides a concept of tenant storage, which stores things like the tenant’s IDs, domains, plan, and you can even store tenant-specific application configuration:

$tenant->put($key, $value);
$tenant->set($key, $value); // alias for put()
$tenant->put(['key1' => 'value1', 'key2' => 'value2']);

You can learn more about this package and view the source code on GitHub at stancl/tenancy. Also, check out the stancl/tenancy documentation for full setup and usage instructions.

Filed in: News


Join the weekly newsletter and never miss out on new tips, tutorials, and more.

Laravel News Partners

Laravel Jobs

Senior Laravel Developer (Full-Stack)
Volunteer Vision GmbH
Full-Stack Developer
Nu Image Medical
Senior Software Developer
Remote (US ONLY)/Kenilworth, NJ
Senior Back-end Laravel API Developer - Immediate Contract (Full Time)
ApproveMe // Document Signing
Web Development Project Manager
Houston, TX
Swyft Filings
Experienced Laravel/VueJS Developer (Freelance)
Remote (preferably in the US)
Prosperly LLC
Senior React / Laravel Developer
The C.A.S.E. Engineering Group