-
-
Save wesamly/20852a46451e611bb99a09de8af95650 to your computer and use it in GitHub Desktop.
Laravel Array Environment Variable
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
MY_KEY=key1|value1,key2|value2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//Save as config/sample.php | |
return [ | |
'key' => collect(explode(',', env('MY_KEY', '')))->pipe(function($collection) { | |
$arr = []; | |
foreach ($collection as $item) { | |
if (strpos($item, '|') === false) { | |
continue; | |
} | |
list($k, $v) = explode('|', $item); | |
$arr[$k] = $v; | |
} | |
return collect($arr); | |
})->toArray() | |
]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$data = config('sample.key'); | |
/* | |
* Returns | |
* ["key1" => "value1", "key2" => "value2"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment