How to set and get config values in Laravel 5

Configuration files make it easy to access the config options across the application. In Laravel there is no exception.  It is very clean approach keeping the all config items in one place.

Create a Config file

To create a custom config file in Laravel, go to config directory and create a php file.  In my case I create a file called image.php, to keep various image dimensions.

After creating the file you can return an empty array

Adding config options

We got our config file, now we want to add some options into our file


We can even use multidimensional arrays.

Using config options

We saw that above methods are for setting the config items in Laravel. Now these new methods are to retrieve value from config options.
To get the large image size for product you can write like this

So the first segment is the config file name, and rest is associate array key and values.

Setting a config value on runtime

When you want to change the config value after the application boots.


Editorial Staff

Editorial Staff at tutsplanet is a dedicated team to write various tutorial articles.