Setting configuration values in your
config files can be very useful when trying to apply settings in an application, such as when you want to set the timezone in your basic
In this shot, I will teach you how to set configuration values from your
config directory with the
config method allows your application to get or set values in all files that are in the
config()method to set values
To set values from files in the
config directory, we will use the
config() method, we use dot notation to get the value we want. The first parameter we use is the name of the file without
.php, and then we drill down like we are trying to access values from an array until we get the specific value we are looking for. The second parameter we pass to the
config() method is the new value.
config() method receives the file and the array/key to be accessed in a dot notation format, as seen in the example as the first parameter and the new value as the second parameter.
config directory, we create a file called
socialhandele.php, with its content as shown below:
<?PHP return [ 'facebook' => [ 'url' => 'https://www.facebook.com/scotchdevelopment', 'username' =>'scotchdevelopment' ], 'twitter' => [ 'url' => 'https://twitter.com/scotch_io', 'username' => 'scotch_io' ] ];
To set or change the Facebook URL in the
socialhandle.php file from your controller, we would use the following code:
$fbUrl = config('socailhandle.facebook.url'=>'https://www.facebook.com/chinweubaelijah');
Notice that we do not add the
.php extension when we use the
socialhandle file. We drill down from the
socialhandle file to the
url key, then pass the new value.
View all Courses