Reputation: 5462
I try to find out why my env()
helper always returns null
. This causes trouble especially in app.php
file, where are env()
helpers widely used by default. Perhaps any mysterious server setting?
My env file:
APP_ENV=production
APP_KEY=base64:mymagickey=
APP_DEBUG=false
APP_LOG_LEVEL=info
APP_URL=http://www.example.com
etc...
EDIT - I tried following:
php artisan cache:clear
php artisan view:clear
php artisan config:cache
and ofcourse, i am using env
helper like this: env('APP_ENV')
But still no success. The wierd part is, that $_ENV
php variable contains every single variable from .env
file.
Upvotes: 155
Views: 124739
Reputation: 323
This is caused by configuration caching and can be temporarily solved with:
php artisan config:cache
Upvotes: 16
Reputation: 41280
Since Laravel 5.2, the env(...)
function will not work after you cached the config.
The Laravel documentation says
If you are using the
config:cache
command during deployment, you must make sure that you are only calling theenv
function from within your configuration files, and not from anywhere else in your application.
So the correct answer would be to
If you are calling env from within your application, it is strongly recommended you add proper configuration values to your configuration files and call env from that location instead, allowing you to convert your env calls to config calls.
And I quoted it from the same documentation
For a quick fix this will do:
php artisan config:clear
But it will fail again as soon as configuration is cached, as should always be the case in production environments.
And now it should be clear why, when you tried config:cache
, it did not help, even though it clears the config prior to caching.
Upvotes: 333
Reputation: 62
Always clear config cache whenever any modification is made on .env or any other config files
php artisan config:clear
Upvotes: 0
Reputation: 482
After trying below commands
php artisan config:clear
php artisan cache:clear
php artisan view:clear
php artisan route:clear
If you still get null from env('APP_ENV')
then try this app()->environment()
. Hope this will don't return null. This works for me
Upvotes: 5
Reputation: 71
Remember to check your .env
file for duplicate variable declarations.
STRIPE_PUBLISHABLE_KEY="why-wont-this-key-get-outputted"
#...
# Because at the bottom of the .env file it is unset like this
STRIPE_PUBLISHABLE_KEY=
Upvotes: 1
Reputation: 6224
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:
.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:
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.
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 callconfig('app.env')
function, whether the last command executed isphp artisan config:clear
orphp 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://stackoverflow.com/a/69707739/2877427
Upvotes: 2
Reputation: 155
php artisan config:clear. Please run this command it will clear the config cache. Its happening because the config file is cached and at the time of caching the values must be null.
Upvotes: 0
Reputation: 1111
Just run this command at your Laravel project root
rm bootstrap/cache/config.php
Upvotes: 20
Reputation: 480
If the accepted answer doesn't fix the issue check if .env file has correct read and write permission given. If the file cannot be read by the framework / library then the values will always be null. I've come across this issue on one of my Lumen projects but after changing the file permissions it worked.
Upvotes: 0
Reputation: 5602
I know this is a very old thread and the reason may not be the same. But the same issue happened to me.
The reason was I had an issue inside .env file. This is what I got in .env
TEST="e@1asa
Notice that ending quote is missing. And it should be like,
TEST="e@1asa"
All the env variables I added after that returned null due to this error.
Nothing written to error log even. So if you have met this error try adding an example env variable as the first record of the file. If it works and record at last not working there may be an error inside .env
Upvotes: 0
Reputation: 1866
The following Command worked for me.
I accidently cleared all the cache files, So env('something')
was returning null.
php artisan optimize:clear
Upvotes: 54
Reputation: 462
Use \Config::get('app.env');
instead of env(APP_ENV);
because you're going to get the same error eventually and that's not good for a live website.
If you want to add custom variables from your ENV, go into your config app and find this:
/*
|--------------------------------------------------------------------------
| Application Environment
|--------------------------------------------------------------------------
|
| This value determines the "environment" your application is currently
| running in. This may determine how you prefer to configure various
| services your application utilizes. Set this in your ".env" file.
|
*/
'env' => env('APP_ENV', 'production'),
add a new line under "'env' => env('APP_ENV', 'production'),
", so for example, it could be the following:
/*
|--------------------------------------------------------------------------
| Application Environment
|--------------------------------------------------------------------------
|
| This value determines the "environment" your application is currently
| running in. This may determine how you prefer to configure various
| services your application utilizes. Set this in your ".env" file.
|
*/
'env' => env('APP_ENV', 'production'),
'key' => env('APP_KEY'),
You can call the "key" variable like this:
\Config::get('app.key');
Whenever you add a new variable like "key" to the app env, you'll need to use config:cache
to reset the cache.
Upvotes: 20