Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle arrays in the configuration? (Laravel)

I created a new configuration file, which is a nested array with string keys. The .env can not store arrays, that is why I created the config/myconfig.php. I access the data with Config::get('myconfig')

Unfortunatelly this config is not the same for every customer. Since I can not put this array into the .env, I manually update the config php file, where it is needed.

What is the best way to define an environment variable, which is not a simple string, but a nested array?

like image 266
Iter Ator Avatar asked Jan 22 '26 04:01

Iter Ator


1 Answers

Got the same trouble and fixed following this post at Laracast and I think it fits your problem too.

Basically I created a custom config file in the config folder, like:

return [     
    env('KEY') => [
        env('KEY_ONE') => env('VALUE_ONE'),     
        env('KEY_TWO') => env('VALUE_TWO')  
    ]
};

And in my .env file:

KEY=VALUE
KEY_ONE=VALUE_ONE
KEY_TWO=VALUE_TWO

Simple as that, then you can access to the array in your application calling the config variable like:

Config::get('configfile_name.key');

or directly to the array index:

Config::get('configfile_name.key.key_one');
like image 187
Troyer Avatar answered Jan 23 '26 20:01

Troyer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!