PHPackages                             ikechukwukalu/dynamicdatabaseconfig - 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. ikechukwukalu/dynamicdatabaseconfig

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

ikechukwukalu/dynamicdatabaseconfig
===================================

This is a laravel package that helps you set database configurations dynamically.

v3.0.0(10mo ago)109511[2 PRs](https://github.com/ikechukwukalu/dynamicdatabaseconfig/pulls)MITPHPPHP &gt;=7.3CI passing

Since Mar 21Pushed 10mo ago2 watchersCompare

[ Source](https://github.com/ikechukwukalu/dynamicdatabaseconfig)[ Packagist](https://packagist.org/packages/ikechukwukalu/dynamicdatabaseconfig)[ RSS](/packages/ikechukwukalu-dynamicdatabaseconfig/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (8)Dependencies (9)Versions (11)Used By (0)

DYNAMIC DATABASE CONFIG
=======================

[](#dynamic-database-config)

[![Latest Version on Packagist](https://camo.githubusercontent.com/2a9fd73b8f12a3701e76722959ca942bc7de1f141fb648cec7b8601b1dc87577/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f696b656368756b77756b616c752f64796e616d69636461746162617365636f6e6669673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/ikechukwukalu/dynamicdatabaseconfig)[![Quality Score](https://camo.githubusercontent.com/eeed71e46b9c4a1ef8e39ec5671ddf6eaf54f323370a81fb8b746271c5f00b99/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f7175616c6974792f672f696b656368756b77756b616c752f64796e616d69636461746162617365636f6e6669672f6d61696e3f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/ikechukwukalu/dynamicdatabaseconfig/)[![Code Quality](https://camo.githubusercontent.com/f0455ccdff6645c5ff4f07797f63f4bf8cc9294e9b4e2f7b0bbaf3d071892853/68747470733a2f2f696d672e736869656c64732e696f2f636f6465666163746f722f67726164652f6769746875622f696b656368756b77756b616c752f64796e616d69636461746162617365636f6e6669673f7374796c653d666c61742d737175617265)](https://www.codefactor.io/repository/github/ikechukwukalu/dynamicdatabaseconfig)[![Known Vulnerabilities](https://camo.githubusercontent.com/6cf1eed65e06a107b0de68d846692e7d94728342e40d3d3540435276d0a2e44d/68747470733a2f2f736e796b2e696f2f746573742f6769746875622f696b656368756b77756b616c752f64796e616d69636461746162617365636f6e6669672f62616467652e7376673f7374796c653d666c61742d737175617265)](https://security.snyk.io/package/composer/ikechukwukalu%2Fdynamicdatabaseconfig)[![Github Workflow Status](https://camo.githubusercontent.com/8fc67e7265848780a146dfe8cd04f5b210fd39873681bd3d4168773fcd1a5320/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f696b656368756b77756b616c752f64796e616d69636461746162617365636f6e6669672f64796e616d69636461746162617365636f6e6669672e796d6c3f6272616e63683d6d61696e267374796c653d666c61742d737175617265)](https://github.com/ikechukwukalu/dynamicdatabaseconfig/actions/workflows/dynamicdatabaseconfig.yml)[![Total Downloads](https://camo.githubusercontent.com/a6408ce2b1f1be04878e21c21289af1b62fa1bd19b288c5b95e33cd823ad2aa6/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f696b656368756b77756b616c752f64796e616d69636461746162617365636f6e6669673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/ikechukwukalu/dynamicdatabaseconfig)[![Licence](https://camo.githubusercontent.com/9f0dbcb85b5b6a1f7e014162e0b799f4a0d132f4078bb5379de5885f75f1efb2/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f696b656368756b77756b616c752f64796e616d69636461746162617365636f6e6669673f7374796c653d666c61742d737175617265)](https://github.com/ikechukwukalu/dynamicdatabaseconfig/blob/main/LICENSE.md)

This laravel package helps you dynamically set more database configurations through the `.env` file or `database`.

REQUIREMENTS
------------

[](#requirements)

- PHP 8.0+
- Laravel 9+

STEPS TO INSTALL
----------------

[](#steps-to-install)

```
composer require ikechukwukalu/dynamicdatabaseconfig
```

### Introduction

[](#introduction)

The need for this package came up when I once handled an already existing project that, due to certain constraints, had 9 databases implemented for each country their application was being utilised. This application also had a central database that was used by every country as well.

The `config/database` file wasn't pretty. I'd prefer to have all configurations within the `.env` file only. The Big question was, what if the databases required grew to 19? These were the problems, both pending and existing that needed a clean hack/solution.

### Middlewares

[](#middlewares)

- `env.database.config`
- `dynamic.database.config`

#### `Env.database.config` Middleware

[](#envdatabaseconfig-middleware)

This middleware fetches database configurations from the `.env` file using postfixes like `ONE`. This dynamically declares an additional database connection for your laravel application.

- Sample env config

```
DB_HOST_ONE=127.0.0.1
DB_PORT_ONE=3306
DB_DATABASE_ONE=second_db
DB_USERNAME_ONE=root
DB_PASSWORD_ONE=
```

- Sample middleware implementation

```
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;

/**
 * mysql is the type of relational database connection being replicated - $database
 * mysql_1 is the new connection name - $name
 * ONE is the postfix - $postfix
 */

Route::middleware(['env.database.config:mysql,mysql_1,ONE'])->group(function () {
    Route::post('/user', function (Request $request) {
        /**
         * $request->_db_connection === 'mysql_1'
         */
        return \App\Models\User::on('mysql_1')->find(1);
    });
});

Route::post('/user', function (Request $request) {
        /**
         * $request->_db_connection === 'mysql_1'
         */
        return \App\Models\User::on('mysql_1')->find(1);
})->middleware('env.database.config:mysql,mysql_1,ONE');
```

You would not need to add a postfix, `ONE`, parameter to the middleware for the `$postFix` variable if you simply set the following session value `session(config('dynamicdatabaseconfig.session_postfix'))`, but when a postfix parameter has been set, it will be used instead of the session value.

#### `Dynamic.database.config` Middleware

[](#dynamicdatabaseconfig-middleware)

This middleware fetches database configurations from the `database_configurations` table within the primary migration database. It utilises a unique `$ref` variable. It's recommended that the unique `$ref` variable should be human readable, that way it becomes easier to run the package's console commands for running migrations. This will also dynamically declare an additional database connection for your laravel application.

- Model file

```
use Ikechukwukalu\Dynamicdatabaseconfig\Models\DatabaseConfiguration;

protected $hidden = [
        'ref',
        'name',
        'database',

        /**
         * Accepts only arrays
         */
        'configuration'
];
```

- Sample eloquent database insert

```
$countries = ['nigeria', 'ghana', 'togo', 'kenya'];
$config = \Config::get('database.connections.mysql');

foreach ($countries as $country) {
    $config['database'] = $country . '_db';
    DatabaseConfiguration::firstOrCreate(
    ['ref' => $country],
    [
        'ref' => $country,
        'name' => 'mysql_' . $country,
        'database' => 'mysql',
        'configuration' => $config
    ]);
}
```

- Sample middleware implementation

```
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;

/**
 * nigeria is $ref value
 */

Route::middleware(['dynamic.database.config:nigeria'])->group(function () {
    Route::post('/user', function (Request $request) {
        /**
         * $request->_db_connection === 'mysql_nigeria'
         */
        return \App\Models\User::on('mysql_nigeria')->find(1);
    });
});

Route::post('/user', function (Request $request) {
        /**
         * $request->_db_connection === 'mysql_nigeria'
         */
        return \App\Models\User::on('mysql_nigeria')->find(1);
})->middleware('dynamic.database.config:nigeria');
```

You would not need to add a ref, `nigeria`, parameter to the middleware for the `$ref` variable if you simply set the following session value `session(config('dynamicdatabaseconfig.session_ref'))`, but when a ref parameter has been set, it will be used instead of the session value.

By default, the values stored within the `configuration` field will be hashed, but you can adjust this from the `.env` file by setting `DB_CONFIGURATIONS_HASH=false`.

### Migration

[](#migration)

It's compulsory to first migrate laravel's initial database.

- `php artisan migrate`

### Other Migrations

[](#other-migrations)

- Default migrations
- Isolated migrations

#### Default Migrations

[](#default-migrations)

This will only migrate files within laravel's default migration path `database/migrations`

```
php artisan env:migrate mysql mysql_1 ONE

php artisan dynamic:migrate nigeria
```

#### Isolated Migrations

[](#isolated-migrations)

This will only migrate files within the specified migration path `database/migrations/folder`

```
php artisan env:migrate mysql mysql_1 ONE --path=database/migrations/folder

php artisan dynamic:migrate nigeria --path=database/migrations/folder
```

#### Both Migrations

[](#both-migrations)

Running the migrations as displayed below will result in the respective database having the migrated data from migrations within `database/migrations` and `database/migrations/folder`.

```
php artisan env:migrate mysql mysql_1 ONE
php artisan env:migrate mysql mysql_1 ONE --path=database/migrations/folder

php artisan dynamic:migrate nigeria
php artisan dynamic:migrate nigeria --path=database/migrations/folder
```

### Database Migration With Seeding

[](#database-migration-with-seeding)

```
php artisan env:migrate mysql mysql_1 ONE --seed
php artisan env:migrate mysql mysql_1 ONE --seeder=DatabaseSeederOne
php artisan env:migrate mysql mysql_1 ONE --seeder=DatabaseSeederOne  --path=database/migrations/folder

php artisan dynamic:migrate nigeria --seed
php artisan dynamic:migrate nigeria --seeder=DatabaseSeederNigeria
php artisan dynamic:migrate nigeria --seeder=DatabaseSeederNigeria  --path=database/migrations/folder
```

### Re-runing Migrations Afresh

[](#re-runing-migrations-afresh)

```
php artisan env:migrate mysql mysql_1 ONE --fresh
php artisan env:migrate mysql mysql_1 ONE --fresh --seed
php artisan env:migrate mysql mysql_1 ONE --fresh --seeder=DatabaseSeederOne
php artisan env:migrate mysql mysql_1 ONE --path=database/migrations/folder --fresh
php artisan env:migrate mysql mysql_1 ONE --path=database/migrations/folder --fresh --seeder=DatabaseSeederOne

php artisan dynamic:migrate nigeria --fresh
php artisan dynamic:migrate nigeria --fresh --seed
php artisan dynamic:migrate nigeria --fresh --seeder=DatabaseSeederNigeria
php artisan dynamic:migrate nigeria --path=database/migrations/folder --fresh
php artisan dynamic:migrate nigeria --path=database/migrations/folder --fresh --seeder=DatabaseSeederNigeria
```

### Refreshing Migrations

[](#refreshing-migrations)

```
php artisan env:migrate mysql mysql_1 ONE --refresh
php artisan env:migrate mysql mysql_1 ONE --refresh --seed
php artisan env:migrate mysql mysql_1 ONE --refresh --seeder=DatabaseSeederOne
php artisan env:migrate mysql mysql_1 ONE --path=database/migrations/folder --refresh
php artisan env:migrate mysql mysql_1 ONE --path=database/migrations/folder --refresh --seeder=DatabaseSeederOne

php artisan dynamic:migrate nigeria --refresh
php artisan dynamic:migrate nigeria --refresh --seed
php artisan dynamic:migrate nigeria --refresh --seeder=DatabaseSeederNigeria
php artisan dynamic:migrate nigeria --path=database/migrations/folder --refresh
php artisan dynamic:migrate nigeria --path=database/migrations/folder --refresh --seeder=DatabaseSeederNigeria
```

### Rolling Back Migrations

[](#rolling-back-migrations)

```
php artisan env:migrate mysql mysql_1 ONE --rollback
php artisan env:migrate mysql mysql_1 ONE --path=database/migrations/folder --rollback

php artisan dynamic:migrate nigeria --rollback
php artisan dynamic:migrate nigeria --path=database/migrations/folder --rollback
```

### Database Seeding

[](#database-seeding)

```
php artisan env:seed mysql mysql_1 ONE --seed
php artisan env:seed mysql mysql_1 ONE --seeder=DatabaseSeederOne

php artisan dynamic:seed nigeria --seed
php artisan dynamic:seed nigeria --seeder=DatabaseSeederNigeria
```

NOTE
----

[](#note)

- A primary database is needed before any other database can be migrated.
- A database will be created if it does not exist.
- Each database will retain it's own independent `migration` table.
- It's recommended that you do not publish the package's migration file, unless you want the `database_configurations` table to be migrated into every extra database created when running **Default migrations**.

PUBLISH MIGRATIONS
------------------

[](#publish-migrations)

- `php artisan vendor:publish --tag=ddc-migrations`

PUBLISH CONFIG
--------------

[](#publish-config)

- `php artisan vendor:publish --tag=ddc-config`

LICENSE
-------

[](#license)

The DDC package is an open-sourced software licensed under the [MIT license](https://opensource.org/licenses/MIT).

###  Health Score

37

—

LowBetter than 83% of packages

Maintenance54

Moderate activity, may be stable

Popularity26

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity46

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 87.5% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~118 days

Recently: every ~185 days

Total

8

Last Release

321d ago

Major Versions

v1.0.4 → v2.0.02024-06-18

v2.0.1 → v3.0.02025-07-01

PHP version history (2 changes)v1.0.0PHP &gt;=8.0

v2.0.0PHP &gt;=7.3

### Community

Maintainers

![](https://www.gravatar.com/avatar/9f17e9c52bd3fcd53771df07c9299bf0e787531d9bfd60daaaeb40f7f03769fe?d=identicon)[ikay007](/maintainers/ikay007)

---

Top Contributors

[![monitecture](https://avatars.githubusercontent.com/u/100952206?v=4)](https://github.com/monitecture "monitecture (28 commits)")[![ikechukwukalu](https://avatars.githubusercontent.com/u/60310041?v=4)](https://github.com/ikechukwukalu "ikechukwukalu (2 commits)")[![nowodev](https://avatars.githubusercontent.com/u/43044740?v=4)](https://github.com/nowodev "nowodev (1 commits)")[![snyk-bot](https://avatars.githubusercontent.com/u/19733683?v=4)](https://github.com/snyk-bot "snyk-bot (1 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/ikechukwukalu-dynamicdatabaseconfig/health.svg)

```
[![Health](https://phpackages.com/badges/ikechukwukalu-dynamicdatabaseconfig/health.svg)](https://phpackages.com/packages/ikechukwukalu-dynamicdatabaseconfig)
```

###  Alternatives

[laravel/cashier

Laravel Cashier provides an expressive, fluent interface to Stripe's subscription billing services.

2.5k25.9M107](/packages/laravel-cashier)[laravel/pulse

Laravel Pulse is a real-time application performance monitoring tool and dashboard for your Laravel application.

1.7k12.1M99](/packages/laravel-pulse)[tucker-eric/eloquentfilter

An Eloquent way to filter Eloquent Models

1.8k4.8M26](/packages/tucker-eric-eloquentfilter)[roots/acorn

Framework for Roots WordPress projects built with Laravel components.

9682.1M97](/packages/roots-acorn)[clickbar/laravel-magellan

This package provides functionality for working with the postgis extension in Laravel.

423715.4k1](/packages/clickbar-laravel-magellan)[aedart/athenaeum

Athenaeum is a mono repository; a collection of various PHP packages

245.2k](/packages/aedart-athenaeum)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
