简体   繁体   中英

Laravel 5.1 create array variable in .env file for global use

Actually, I want an array like this in .env file. I do not have any idea to create an array variable in .env file.

 VARIABLE_NAME = [
    [0] => 'Value 1',
    [1] => 'Value 2',
    [2] => 'Value 3',
    ................

];

You can't store an array in .env file as the ENV format doesn't support that.

A workaround could be serializing the array to a string of some known format, eg comma separated values, and then split it whenever you needed.

This should do the trick:

#.env file
VARIABLE_NAME="Value 1,Value 2,Value 3"

#config/app.php
return [
  'VARIABLE_NAME' => explode(',', env('VARIABLE_NAME'))
];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM