How to call config method in a file Laravel

Config Files are under the config directory, and can be assigned with values during runtime of a Laravel application using the config() method. You can call config method and pass it two parameters to get or set their values. The dot notation will help retrieve the desired value. Here the first parameter is the file’s name without adding .php to it. Next we write as we try to access values from a array till we get a specific value we need. The second parameter for the config() method is the new value we need to set to.

The syntax of the config helper method is as shown below.

config('social.twitter.url', 'http://twitter.com');

This takes the variable value (URL) from files under config directory, i.e. social.twitter.url, here the variable is the twitter URL taken from socialhandele.php file. We read this data from the configuration file, where it comes up as shown below.

<?PHP 
return [ 
'twitter' => [ 
'url' => 'https://twitter.com/scotch_io',
'username' => 'mary_janes' ] ];

You can retrieve and change the URL from this file (socialhandele.php) when you call config method.

The config helper method also allows us to change an array of values, as shown in the example below.

// Change service config parameters at runtime  laravel

You can set config values dynamically at runtime with config() helper:

config(['services.mailgun' => $arrayWithNewSettings]);

You can save settings details in an array and pass that variable as a second parameter to the config helper function alongside the variable you want to change.

Leave a Comment