How to create custom configuration in laravel

Rakesh Sharma      2 Comments  

How to create custom configuration in laravel :-

All configuration files of Laravel framework stored in the app/config directory.
so if we need to create custom configuration values it would be better to keep separate our custom configuration in custom file.
so we need to create custom file in app/config directory. laravel auto read this file as a config file and will auto manage it
In this topic we are working with custom configuration in laravel and get configuration value in controller or view.

Create custom configuration in laravel :-

create a file in app/config/custom.php which have config keys and value like:-

return array(
  'my_val' => 'mysinglelue',
  'my_arr_val' => array('1', '2', '3'),

Now need to get these config values in view/controller so we will use Config class get() method for this
syntax :- echo Config::get('filename.arraykey');
where filename is the config file’s name, custom in our case, and key is the array key of the value you’re wanting to access.

echo Config::get('custom.my_val');

Create run time configuration in laravel :-

Configuration values which are set at run-time are will set for the current request, not be carried over to subsequent requests.

Config::set('custom.my_val', 'mysinglelue');

For more information about laravel configuration.

  • Ashish Kadam

    How can we fire an eloquent query in custom config file for Laravel 5, it was possible in Laravel 4.x, but its not working for 5 the same way?

    • rakesh sharma

      if you run query in config you need to initialize connection object first then run your query with connection object. i think db connection not initialized in config so you getting error