Dynamic configuration from database in Laravel 5.4

We are going to discuss about how we will do dynamic configuration variable and values with laravel from a database like MySQL. Because most of the web sites required some of frequent dynamic config variables like commission percentage, processing fee and etc..

But the idea is to solve this problem without making a code change or new build of the application. So we just change the configuration value of the database and it gets affected across the website.

Lets start with database schema, We have two columns which is config_key and config_value. So config_key holds the config key name and config_value holds the value.

 

Screenshot from 2017-06-01 18-00-36

 

Stored Values like this

Screenshot from 2017-06-01 18-02-06

 

 

 

 

 

 

Create a Model

We will use model to retrieve and store the configuration data.

Then next we need to write a middleware to distribute the configuration to across application. Here mentioned below middleware.

Then we need to add this middleware at Http/Kernal.php

Then all the routes must use this middleware. So that the dyanmic config has been available

Example routes:

For the performance we have used cache things in the middleware. Now configuration has been available across application and you can access it as follows.

Note: If you are caching the configuration then you must clear the cache after made the configuration changes.

Had any doubts and clarification. Please feel free to put it in comment box.

  • Jitendra Prajapati

    something unique.! good job boss.!

    • azhagupandian

      thank you

  • saladin

    GREAT STUFF