How to use Config file in Laravel

All of the configuration files for the Laravel framework are stored in the config directory. There are a lot of configuration files by default such as database, mail, queue and so on. We can create our own config file that can be used to set options in our application.

Create Config file

To create a configuration file, we just need to create a file inside config folder and return an array with key-value pair.

We can also use multi-dimensional array to set the value.

Note that the second example pick up the value from .env file. If this is not set, it will take default value true. So, we just need to set option as below in .env file.

Get Config value

We can get the cofiguration values using the Config facade. For example:

There is also another method to get config value. We can use config function. For example:

Be sure to clear the cached config because config files are cached for faster performance. To know more, here is a nice article explaining all the methods about clearing cache in laravel.

That’s it. Isn’t it pretty easy to set and retrieve cofiguration file in laravel? Drop your thoughts below.