Laravel 5: Accessing and Manipulating Configuration Values With config

April 14, 2018 —John Koster

The config helper function can be used to accomplish many different tasks, depending on the types of arguments, and number of arguments supplied to the function. For example, the config function can be used to resolve an instance of the configuration repository from the Service Container, or to get or set configuration values at runtime.

#Signature

The signature of the config function is:

1function config();
2 
3function config([
4 $key,
5 $value = null
6]);
7 
8function config(
9 $key = null,
10 $default = null
11);

#Example Use

The following example shows how to resolve an instance of "Illuminate\Config\Repository" from the Service Container:

1// Calling the `config` function without any arguments
2$configRepository = config();

After the above code has executed, the value of $configRepository will be, by default, an instance of "Illuminate\Config\Repository" (more specifically, the return value will be whatever class instance is bound to the config alias within the service container). This can be used a shortcut to accessing the configuration repository (versus injecting an instance of the configuration repository or using the Config facade):

1// Using the `config` function to return an instance of
2// `Illuminate\Config\Repository` to check if a config item exists.
3if (config()->has('someKey')) {
4 // Do something if the `someKey` config item exists.
5}

#Setting Configuration Values

The config function can be used to set configuration values if the provided $key is an array, and no $default value is supplied (or the $default value supplied is null). If multiple items are passed in the array, all key/value pairs will added to the configuration:

1// New config items to store.
2$newConfigurationValues = [
3 'keyOne' => 'valueOne',
4 'keyTwo' => 'valueTwo',
5 'keyThree' => 'valueThree'
6];
7 
8// Store all three key/value pairs in the config.
9config($newConfigurationValues);

In the above example all three key/value pairs will be stored in the configuration separately. The following example highlights how the values are returned (by returning an instance of the configuration repository). The value returned will appear above the function call as a comment:

1// valueOne
2config()->get('keyOne');
3 
4// valueTwo
5config()->get('keyTwo');
6 
7// valueThree
8config()->Get('keyThree');

#Retrieving Configuration Values

The config function can even be used to retrieve configuration values, as long as the provided $key is not an array and not null. A $default value can be supplied that will be returned if the configuration $key does not exist. The following examples will use the $newConfigurationValues from the previous section. The value returned will appear above the function call as a comment:

1// valueOne
2$retrievedValueOne = config('keyOne');
3 
4// valueTwo
5$retrievedValueTwo = config('keyTwo');
6 
7// valueThree
8$retrievedValueThree = config('keyThree');
9 
10// Not null
11$retrievedValueFour = config('nonExistent', 'Not null');

Some absolutely amazing
people

The following amazing people help support this site and my open source projects ♥️
If you're interesting in supporting my work and want to show up on this list, check out my GitHub Sponsors Profile.