Laravel 5 - env() always returns null

PhpLaravelEnvironment VariablesPhpdotenv

Php Problem Overview


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.

Php Solutions


Solution 1 - Php

env(...) function will not work after you cached the config. (starting from laravel 5.2 till current 5.7)

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

But for a quick fix this will do:

> php artisan config:clear

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.

Solution 2 - Php

Hope this command will save you

> php artisan config:clear

Solution 3 - Php

Five most important commands if your Laravel is not working as expected after some modifications in .env or database folder or because of any other modifications. Here is full explanation: https://www.youtube.com/watch?v=Q1ynDMC8UGg

php artisan config:clear
php artisan cache:clear
php artisan view:clear
php artisan route:clear
composer dump-autoload

Solution 4 - Php

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

php artisan optimize:clear

Solution 5 - Php

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.

Solution 6 - Php

It is a ".env" known bug which can be solved with:

php artisan config:cache

Solution 7 - Php

Just run this command at your Laravel project root

rm bootstrap/cache/config.php

Solution 8 - Php

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://stackoverflow.com/a/69707739/2877427

Solution 9 - Php

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

Solution 10 - Php

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.

Solution 11 - Php

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=

Solution 12 - Php

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

Solution 13 - Php

Place .env file at the root folder of the project

Add these to app.php file.

try {
    (new Dotenv\Dotenv(__DIR__.'/../'))->load();
} catch (Dotenv\Exception\InvalidPathException $e) {
    echo $e;
}

Solution 14 - Php

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionFusionView Question on Stackoverflow
Solution 1 - PhpYevgeniy AfanasyevView Answer on Stackoverflow
Solution 2 - Phpsh6210View Answer on Stackoverflow
Solution 3 - PhpAbhay MauryaView Answer on Stackoverflow
Solution 4 - PhpSantosh KumarView Answer on Stackoverflow
Solution 5 - PhpDIGITALCRIMINALView Answer on Stackoverflow
Solution 6 - PhpvpdevaView Answer on Stackoverflow
Solution 7 - PhpIgor SazonovView Answer on Stackoverflow
Solution 8 - PhpAnasSafiView Answer on Stackoverflow
Solution 9 - PhpvimuthView Answer on Stackoverflow
Solution 10 - PhpSachithdView Answer on Stackoverflow
Solution 11 - PhpjohncarterView Answer on Stackoverflow
Solution 12 - PhpLokman HosenView Answer on Stackoverflow
Solution 13 - PhpThushara BuddhikaView Answer on Stackoverflow
Solution 14 - Phpvivek rautelaView Answer on Stackoverflow