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.

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-db-tenant

Laravel DB Tenant

GitHub Release

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:

Features

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:

*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:

You can also manually reset the stack at any time:

Credits

License

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


All versions of laravel-db-tenant with dependencies

PHP Build Version
Package Version
Requires php Version ^8.3
laravel/framework 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 aloisogomes/laravel-db-tenant contains the following files

Loading the files please wait ...