Download the PHP package aloisogomes/laravel-db-tenant without Composer
On this page you can find all versions of the php package aloisogomes/laravel-db-tenant. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download aloisogomes/laravel-db-tenant
More information about aloisogomes/laravel-db-tenant
Files in aloisogomes/laravel-db-tenant
Package laravel-db-tenant
Short Description Library to help you have multiple connections to the database while keeping the context clean without changing anything in the structure of your tables
License MIT
Informations about the package laravel-db-tenant
Laravel DB Tenant
Laravel DB Tenant is a lightweight, elegant package that allows you to switch database connections "on the fly" for specific blocks of code. Unlike global configuration changes, this package uses a Context Stack, allowing for nested connection switches while ensuring Eloquent models remain "sticky" to the connection they were created in.
It is perfect for:
- Multi-tenant applications with separate databases per client.
- Data migration scripts moving data between connections.
- Archival systems where you need to access historical data on a separate DB.
Features
- Context Stacking: Supports nested
start()andend()calls. - Sticky Connections: Models instantiated inside a tenant block "remember" their connection, even after the block ends.
- Safe Transactions: Helper to run transactions on the current active context.
- Queue & Request Safety: Automatically resets the connection stack after every HTTP request and Queue Job to prevent data leaks.
- Zero Config: Works out of the box with standard Laravel database configurations.
Before you start..
This package assumes that your connections are to separate databases, but with the same table structure, precisely to take advantage of the rules defined in your models and business logic. Think, for example, of a store platform, where each store has its own database and works separately on different servers, etc... But you want to give an accountant a single platform where he can obtain data from each of the stores in just one place.
This package is not for you if you expect:
- Create complex relationships between tables from different databases (joins, unions, views, etc.);
- Harmonize data from different tables;
- Alert, handle or ensure data consistency involving distinct connections;
- Manage transactions and locks in operations involving objects from different databases*;
- Create replications, mirroring, or backups**
*You can create a script that orchestrates an operation flow ensuring the order and criteria for separate transactions, but it is not the responsibility of this package to manage that flow.
**You may create a listener in your application to manage events in your models and use this package to update another database, but handling failures or ensuring consistency is out of the scope of this project. The responsibility for these flows remains with the developer who desires this behavior.
Installation
You can install the package via composer:
Setup
Configure your known connections in config/database.php at connections list:
Add the HasTenantConnection trait to any Eloquent Model you want to be tenant-aware.
That's it. Your model is now ready to react to context switches.
Usage
1. Basic Context Switching
Use the Tenant facade to define the context. Any model retrieved inside the block will use the specified connection.
2. Sticky Connections (The Power Feature)
Models created within a tenant context retain their connection reference forever. You can manipulate and save them safely outside the tenant block.
3. Nested Contexts (Stacking)
The package manages a LIFO (Last In, First Out) stack.
4. Explicit Override
If you need to force a specific connection regardless of the current context, use the tenant() static method (alias for on):
5. Transactions
To safely run transactions within the current active context (whether it is the default or a tenant), use the transaction helper:
Safety Measures
One of the biggest risks with runtime connection switching is "polluting" the state for subsequent requests (e.g., in Laravel Octane or Queue Workers).
This package automatically handles cleanup:
- HTTP Requests: The stack is reset when the app terminates (after the response is sent).
- Queue Jobs: The stack is reset after every Job is processed or failed.
You can also manually reset the stack at any time:
Credits
License
The MIT License (MIT). Please see License File for more information.