3

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?

3
  • 1
    There's no reason you can't store JSON strings in your .env file if you need to. You can call json_decode() on it. But be ware, this may have unintended consequences. Commented Apr 5, 2018 at 9:08
  • Maybe this could help you. Commented Apr 5, 2018 at 9:22
  • Just curious, what kind of environment variable are you trying to store as an array? Are the values not scalar? Commented Apr 5, 2018 at 10:22

1 Answer 1

4

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');
Sign up to request clarification or add additional context in comments.

3 Comments

thanks a lot for helping and it is working according to my requirements
i want to get all configs in one of the config file as an array
Actually, you'd need to access values with Config::get('configfile_name.VALUE.VALUE_ONE'), which would give you a null result since you don't define VALUE_ONE in your .env file. Nevertheless, the principle works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.