Patrique Ouimet
Senior Product Engineer
Mon, Feb 18, 2019 9:55 AM
There's a few ways to access the nested data in configs, consider the following config file config/mail.php (redacted for demo purpose):
<?php
return [
'markdown' => [
'theme' => 'default',
'paths' => [
resource_path('views/vendor/mail'),
],
],
];If we wanted to access the value for theme there's a few approaches you can take.
Here we get the config file then access the nested values with array keys
config('mail')['markdown']['theme']; // returns 'default'The issue with the above is if by chance you tried to access something that didn't exist it would throw an exception similar to Undefined index, here's an example
config('mail')['foobar']; // exception "Undefined index"With the dot notation even if the array key doesn't exist it'll return null
config('mail.markdown.theme'); // returns 'default'
config('mail.foobar'); // returns nullI also find the syntax more appealing
Do note that using the array method MAY be advantageous as it may warm you to potentially misconfigured config files. In a perfect world you'd have tests that would cover that scenario ;). Also, you can replace config with Config::get if you feel more comfortable using the facade (there's also other ways to access the config i.e. app('config')->get('mail.markdown.theme')). Thanks for reading, hope this helps!