Unfortunately, the accepted solution does not provide an appropriate solution for this problem!
The case: Since Laravel 5.2
, if you call env("key")
directly, it will always return null
if you executed php artisan config:cache
as last command.
Accepted answer say : execute php artisan config:clear
as last command to solve problem! This is not a solution!, because we need always to execute php artisan config:cache
as last command..
So what is the solution?
Here i need to explain solution for two of cases:
Case 1 (Predefined keys): If you need to get value from .env
file which created by Laravel project as (default .env file), or by third party plugin you already installed it and follow the correct installation procedure of installation:
- Get the key name which listed in
.env
file and search for it in files of laravel_root/config
folder (remember: all files in config
folder returns results as array), for example i searched for APP_URL
which per-defined by Laravel project as default:
As you say here I found tow results in tow config files, one in filesystem.php
and other in app.php
, so to get any of these values in any php (Classes or view files, ... etc) on your laravel project just call it like this:
- For result which we found in
app.php
file, get it value by:
config('app.env');
Here app
is name of php file, and env is the key of array which returned as result of app.php
file.
- For result which we found in
filesystems.php
file, get it value by:
config('filesystems.public.url');
Here filesystems
is name of php file, and public
is the key (level 1) and url
is the key (level 2) of array which returned as result of filesystems.php
file.
Summary of this case: if you need get value of APP_URL
in any php file of Laravel project you can get
it by call config('app.env')
function, whether the last command executed
is php artisan config:clear
or php artisan config:cache
, It
doesn't matter at all.
Case 2 (Not predefined keys - if you need generate new custom environment key): See this answer for explain: https://mcmap.net/q/160577/-get-environment-value-in-controller
php artisan config:clear
is the only thing that worked to get my AWS SES credentials to read. – Briarwood