PHPackages                             worldesports/laravel-auto-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. [Database &amp; ORM](/categories/database)
4. /
5. worldesports/laravel-auto-tenancy

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

worldesports/laravel-auto-tenancy
=================================

Laravel package for post-authentication multi-tenancy with per-tenant database isolation and automatic connection switching

v1.0.0(3mo ago)3201[3 PRs](https://github.com/World-eSports-LLC/laravel-auto-tenancy/pulls)MITPHPPHP ^8.2|^8.3|^8.4CI passing

Since Mar 30Pushed 2mo agoCompare

[ Source](https://github.com/World-eSports-LLC/laravel-auto-tenancy)[ Packagist](https://packagist.org/packages/worldesports/laravel-auto-tenancy)[ Docs](https://github.com/World-eSports-LLC/laravel-auto-tenancy)[ GitHub Sponsors](https://github.com/World-eSports-LLC)[ RSS](/packages/worldesports-laravel-auto-tenancy/feed)WikiDiscussions remote Synced 3w ago

READMEChangelogDependencies (14)Versions (5)Used By (0)

Laravel Auto Tenancy
====================

[](#laravel-auto-tenancy)

Post-authentication multi-tenancy for Laravel applications with runtime tenant connection resolution and automatic database switching after login.

[![Latest Version on Packagist](https://camo.githubusercontent.com/51e51d5fdede509caa0bb8266a2b31da3b019f94ee9c761917b68893c10f2c76/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f776f726c646573706f7274732f6c61726176656c2d6175746f2d74656e616e63792e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/worldesports/laravel-auto-tenancy) [![GitHub Tests Action Status](https://camo.githubusercontent.com/8c8fe4b0ea24db5054b5d9ad42c9825f18379e03800315494a69ab1847315914/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f576f726c642d6553706f7274732d4c4c432f6c61726176656c2d6175746f2d74656e616e63792f72756e2d74657374732e796d6c3f6272616e63683d72656d6f7465266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/World-eSports-LLC/laravel-auto-tenancy/actions?query=workflow%3Arun-tests+branch%3Aremote) [![GitHub Code Style Action Status](https://camo.githubusercontent.com/ae0e8c0a6e5194f81c83caa3c84b30d33f506035637ed67375daf5d1850b766b/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f576f726c642d6553706f7274732d4c4c432f6c61726176656c2d6175746f2d74656e616e63792f6669782d7068702d636f64652d7374796c652d6973737565732e796d6c3f6272616e63683d72656d6f7465266c6162656c3d636f64652532307374796c65267374796c653d666c61742d737175617265)](https://github.com/World-eSports-LLC/laravel-auto-tenancy/actions?query=workflow%3A%22Fix+PHP+code+style+issues%22+branch%3Aremote) [![PHPStan](https://camo.githubusercontent.com/e682691808fd38ebfee4863ff22f0db874662f42803940e6cafd63df65c577e2/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f576f726c642d6553706f7274732d4c4c432f6c61726176656c2d6175746f2d74656e616e63792f7068707374616e2e796d6c3f6272616e63683d72656d6f7465266c6162656c3d7068707374616e267374796c653d666c61742d737175617265)](https://github.com/World-eSports-LLC/laravel-auto-tenancy/actions?query=workflow%3APHPStan+branch%3Aremote) [![Total Downloads](https://camo.githubusercontent.com/8618191ce5c048dff32d995616bd796e95212385ad02c663c32ddbd0b9aef289/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f776f726c646573706f7274732f6c61726176656c2d6175746f2d74656e616e63792e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/worldesports/laravel-auto-tenancy)

What makes this package different?
----------------------------------

[](#what-makes-this-package-different)

Most Laravel multi-tenancy packages are commonly used in domain-first or subdomain-first workflows, where the tenant is identified from the request before or independently of user authentication.

`worldesports/laravel-auto-tenancy` is built for a different workflow:

1. The user logs in or registers first.
2. The application determines the tenant from the authenticated user.
3. The package chooses or builds the correct database connection at runtime for that tenant.
4. The application is automatically switched into the correct tenant database/context.

This makes it a strong fit for Laravel applications where tenancy is resolved **after authentication** rather than solely from the incoming request host.

### Example use case

[](#example-use-case)

Your app has a shared login screen for all users.

After authentication:

- `alice@acme.com` should use the ACME tenant database
- `bob@globex.com` should use the Globex tenant database
- tenant context is determined from the authenticated user
- the package automatically switches the application into the correct tenant context

Good Fit for This Package
-------------------------

[](#good-fit-for-this-package)

Use this package if your app needs to:

- resolve tenancy after login or registration
- determine the tenant from the authenticated user
- choose or build the correct tenant database connection at runtime
- switch tenant databases automatically
- add tenancy to an existing Laravel auth flow without redesigning the app around domain-first tenancy
- integrate with existing multi-tenant systems

When This May Not Be the Right Fit
----------------------------------

[](#when-this-may-not-be-the-right-fit)

If your application is primarily domain-first or subdomain-first and you need broader tenant bootstrapping beyond authenticated-user-driven database switching, another tenancy approach may be a better fit.

Features
--------

[](#features)

- **Post-authentication multi-tenancy**: Tenants are automatically detected and switched after user login
- **Runtime connection resolution**: The package chooses or builds the correct tenant connection for the authenticated user
- **Multiple database support**: Each tenant can use a separate database connection
- **Minimal configuration**: Install quickly with sensible defaults
- **Automatic tenant switching**: Middleware automatically resolves and switches tenant context
- **Model scoping**: Traits for tenant-aware model queries
- **Database isolation**: Complete separation between tenant data
- **Multi-driver support**: MySQL, PostgreSQL, SQLite, and SQL Server
- **Connection caching**: Optimized database connection management
- **Encryption support**: Optional encryption for sensitive connection details
- **Comprehensive commands**: Full set of Artisan commands for tenant management
- **Event listeners**: Automatic tenant creation and cleanup
- **Security features**: Access control and validation
- **Testing suite**: Comprehensive test coverage

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

[](#installation)

You can install the package via composer:

```
composer require worldesports/laravel-auto-tenancy
```

Quick installation with setup:

```
php artisan tenant:install --force --migrate
```

If authentication is missing, the installer will prompt you to install Jetstream:

```
composer require laravel/jetstream
php artisan jetstream:install livewire
npm install && npm run build
php artisan migrate
```

Or manual installation:

```
# Publish and run the migrations
php artisan vendor:publish --tag="multi-tenancy-migrations"
php artisan migrate

# Optionally, publish the config file
php artisan vendor:publish --tag="multi-tenancy-config"
```

The published config file (`config/multi-tenancy.php`) contains:

```
