Download the PHP package ikechukwukalu/dynamicdatabaseconfig without Composer
On this page you can find all versions of the php package ikechukwukalu/dynamicdatabaseconfig. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download ikechukwukalu/dynamicdatabaseconfig
More information about ikechukwukalu/dynamicdatabaseconfig
Files in ikechukwukalu/dynamicdatabaseconfig
Package dynamicdatabaseconfig
Short Description This is a laravel package that helps you set database configurations dynamically.
License MIT
Informations about the package dynamicdatabaseconfig
DYNAMIC DATABASE CONFIG
This laravel package helps you dynamically set more database configurations through the .env
file or database
.
REQUIREMENTS
- PHP 8.0+
- Laravel 9+
STEPS TO INSTALL
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
env.database.config
dynamic.database.config
Env.database.config
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
- Sample middleware implementation
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
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
-
Sample eloquent database insert
- Sample middleware implementation
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
It's compulsory to first migrate laravel's initial database.
php artisan migrate
Other Migrations
- Default migrations
- Isolated migrations
Default Migrations
This will only migrate files within laravel's default migration path database/migrations
Isolated Migrations
This will only migrate files within the specified migration path database/migrations/folder
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
.
Database Migration With Seeding
Re-runing Migrations Afresh
Refreshing Migrations
Rolling Back Migrations
Database Seeding
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
php artisan vendor:publish --tag=ddc-migrations
PUBLISH CONFIG
php artisan vendor:publish --tag=ddc-config
LICENSE
The DDC package is an open-sourced software licensed under the MIT license.
All versions of dynamicdatabaseconfig with dependencies
illuminate/database Version ^8.0|^9.0|^10.0|^11.0
illuminate/http Version ^8.0|^9.0|^10.0|^11.0
illuminate/routing Version ^8.0|^9.0|^10.0|^11.0
illuminate/support Version ^8.0|^9.0|^10.0|^11.0
php Version >=7.3