Laravel - env() always returns null
Asked Answered
T

13

148

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.

Tensive answered 6/4, 2017 at 0:27 Comment(0)
B
320

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 the env 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.

Baber answered 20/11, 2018 at 2:32 Comment(5)
Thank you, php artisan config:clear is the only thing that worked to get my AWS SES credentials to read.Briarwood
So I think we should not directly use something like env('APP_ENV') in our application. Instead, we should write a line in config/app.php: 'env' => env('APP_ENV'),. Then, we use it in our code: config('app.env').Fortson
@cosmos for anyone reading this, ALWAYS do that, env is only useful in any config/*.php file, never outside !!!!Altis
"There are no commands defined in the "config" namespace." Still Laravel does not read the .env fileInattentive
cache clear did it for meSalimeter
C
61

Hope this command will save you

php artisan config:clear

Cornell answered 26/4, 2018 at 7:44 Comment(0)
H
49

The following Command worked for me. I accidently cleared all the cache files, So env('something') was returning null.

php artisan optimize:clear
Haematoxylin answered 2/10, 2019 at 4:36 Comment(5)
THANK YOU!! This suddenly happened to me today -Laravel 8. This fixed it.Eddins
This is the only answer that worked for me on Laravel 8. Not being able to get the env values was driving me nuts. Thank you very much.Melvamelvena
For some reason all commands like php artisan 'insertsomething':clear doesn't work, only this one worked for meKeesee
Still working this advice ;)Overdo
Laravel 8 gang too, thanks!Procambium
B
20

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.

Bobette answered 10/7, 2017 at 8:10 Comment(2)
I am not sure but may be this bug only occurs in windows. but your solution resolved it in windows too. will it work in linux production environment?Conjecture
@AmitShah, it is not a bug, it is a feature. Please check out my answer for details.Baber
C
17

Just run this command at your Laravel project root

rm bootstrap/cache/config.php
Coroneted answered 22/2, 2021 at 20:31 Comment(1)
What a magic <3Inedited
C
15

This is caused by configuration caching and can be temporarily solved with:

php artisan config:cache
Clayborn answered 6/4, 2017 at 0:31 Comment(4)
Some users may need to do php artisan config:clear insteadBobette
Laravel 5.5 I had to run 'php artisan config:clear'. ThanksSalt
php artisan config:cache is the reason why env() values are null. If you wish to cache your config and env vars then you shouldn't use env() anywhere else except config files because once cached env() will always return null. Although now I am also seeing config() and Config::get() helper return null after caching which is weird too.Lindo
follow up to above - ...So thats when you should realize that config() helper cannot directly access values in .env either. You will need to create or use existing config/<insert config file name>.php config file and create your config entry from .env and then use that mapped value.Lindo
M
5

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

Marashio answered 16/2, 2022 at 8:38 Comment(0)
E
2

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:

  1. 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:

enter image description here

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

Emblematize answered 25/10, 2021 at 12:5 Comment(0)
P
1

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=
Pines answered 17/11, 2021 at 16:13 Comment(0)
S
0

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

Saccharoid answered 2/5, 2020 at 14:32 Comment(0)
I
0

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.

Inextinguishable answered 28/5, 2020 at 22:12 Comment(0)
F
0

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.

Ferminafermion answered 16/9, 2021 at 6:34 Comment(0)
Z
0

Always clear config cache whenever any modification is made on .env or any other config files

php artisan config:clear
Zelma answered 24/7, 2022 at 20:47 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Crib

© 2022 - 2024 — McMap. All rights reserved.