Download the PHP package worldesports/laravel-auto-tenancy without Composer

On this page you can find all versions of the php package worldesports/laravel-auto-tenancy. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package 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 GitHub Tests Action Status GitHub Code Style Action Status PHPStan Total Downloads

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

Your app has a shared login screen for all users.

After authentication:

Good Fit for This Package

Use this package if your app needs to:

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

Installation

You can install the package via composer:

Quick installation with setup:

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

Or manual installation:

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

Basic Usage

How the package decides which tenant database to use

The package does not scan existing databases to guess where a user belongs. It uses the central/default Laravel database as the source of truth.

Default mapping:

That means tenant:create is the provisioning step that tells the package which tenant and database belong to a user:

After that, when user ID 123 authenticates and reaches a route using the tenant middleware, the package finds tenants.user_id = 123, chooses that tenant's database, builds the Laravel database connection at runtime, and switches the request to that connection.

Tenant database selection order:

Existing Laravel apps must create these tenant mappings for existing users. The package intentionally does not auto-discover tenant databases by searching every database for matching users.

1. User Model Configuration

The package automatically works with your existing User model. If you're using a custom User model or different namespace:

2. Authentication System Compatibility

Laravel Breeze

Laravel Jetstream

Laravel Sanctum API

Social Authentication

Setup and Usage

1. Register the middleware (optional)

Add the tenant middleware after authentication on routes that need tenant database context:

Use tenant.required when a route must fail if no tenant was resolved:

The login listener can set tenant context during the login request, but middleware is what resolves tenant context on later web/API requests.

2. Add traits to your models

For models that should be automatically scoped to the current tenant database:

For models that have a tenant_id column and need tenant-based scoping:

Usage

Package Status and Management

Creating Tenants

Use the artisan command to create a new tenant. The package supports all major database drivers:

If the database already exists, pass the tenant database credentials and omit --create-db. If you want the command to create the database, pass --create-db plus root/admin credentials. Root/admin credentials are used only by the command and are not stored for runtime tenant switching.

MySQL Tenant (Default)

To create the database too:

PostgreSQL Tenant

SQLite Tenant

SQL Server Tenant

Multi-Driver Support

Your Laravel application can have tenants using different database drivers simultaneously:

The package automatically handles driver-specific configurations and optimizations.

Database Management

Tenant migrations must live separately from central Laravel/package migrations. By default the package expects:

This prevents central tables like tenants and tenant_databases from being created inside tenant databases.

Tenant Cleanup

Working with Tenants in Code

Querying for a specific tenant (and database) without changing the global context

Note: The package keeps one active tenant database at a time per request/context. You can pick which tenant DB to use, but queries execute against a single selected database, not multiple concurrently.

Tenant Resolution

By default, the package resolves tenants after authentication by matching the authenticated user's primary key to tenants.user_id. That is the recommended production path because it does not trust hostnames or email domains for tenant membership.

When user ID 123 logs in and the tenant middleware runs, the package loads that user's tenant, builds the tenant database connection from the stored tenant_databases.connection_details, and switches the request to that connection.

For existing applications, installing the package does not automatically assign existing users to tenant databases. Create tenant mappings with tenant:create for each user, organization, store, workspace, or customer that should receive a tenant database.

Optional Strategy: Email Domain Detection

Email-domain detection is available, but it is disabled by default. Enable it only if your application treats matching email domains as authorized tenant membership:

Configuration:

Also enable domain-based access if non-owner users should be authorized by exact email-domain match:

Optional Strategy: Subdomain Detection

Subdomain detection is available, but disabled by default and should be paired with Laravel trusted-host protection:

Configuration:

Optional Strategy: Auto-Create Tenants

Auto-create is disabled by default. For production v1 usage, prefer tenant:create because it explicitly provisions the tenant database credentials used for runtime switching:

Configuration:

tenant:create remains the provisioning path for database credentials. Runtime requests do not use root/admin credentials.

Using the Middleware

The SetTenant middleware resolves tenant context for authenticated users. It is non-enforcing by default; use tenant.required on routes that must have tenant context.

Middleware options:

Advanced Configuration

Common Configuration

After publishing config/multi-tenancy.php, common production settings include:

Event Listeners

The package automatically registers these event listeners:

Security Features

Model Scoping Examples

Multi-User & Concurrent Session Support

✅ Multiple Users, Different Tenants

Each authenticated request resolves its own tenant context:

✅ Request Isolation

✅ API & Web Support

Example 1: Social Media Authentication with Multi-Tenancy

Example 2: API Authentication with Sanctum

Example 3: Custom Authentication Guard

Example 4: Multi-Guard Authentication

Example 5: Automated Tenant Creation for New Users

Testing

Changelog

Please see CHANGELOG for more information on what has changed recently.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.


All versions of laravel-auto-tenancy with dependencies

PHP Build Version
Package Version
Requires spatie/laravel-package-tools Version ^1.16
ext-pdo Version *
php Version ^8.2|^8.3|^8.4
illuminate/contracts Version ^10.0|^11.0|^12.0
illuminate/support Version ^10.0|^11.0|^12.0
illuminate/database Version ^10.0|^11.0|^12.0
illuminate/auth Version ^10.0|^11.0|^12.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package worldesports/laravel-auto-tenancy contains the following files

Loading the files please wait ...