Problem
I’m trying to figure out why my env() helper returns null every time. This is particularly problematic in the app.php file, since env() helpers are extensively used by default. Maybe there’s some enigmatic 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: Here’s what I came up with:
php artisan cache:clear
php artisan view:clear
php artisan config:cache
And, of course, I’m using env helper in the following way: env(‘APP ENV’)
However, there has been no success. The strange thing is that the $_ENV php variable has all of the variables from the.env file.
Asked by Fusion
Solution #1
After you cached the settings, the env(…) function will not operate. (beginning with Laravel 5.2 and ending with Laravel 5.7)
According to the documentation for Laravel,
As a result, the right response is
I also cited it from the same source.
However, for a quick workaround, this will suffice:
It should now be evident why config:cache did not assist, despite the fact that it clears the settings before caching.
Answered by Yevgeniy Afanasyev
Solution #2
I hope this command comes in handy.
Answered by sh6210
Solution #3
If your Laravel isn’t working as intended after making changes to the.env or database folders, or after making any other changes, run these five commands. Here’s the whole story: 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
Answered by Abhay Maurya
Solution #4
For me, the following command worked. Because I accidentally deleted all of the cache files, env(‘something’) returned null.
php artisan optimize:clear
Answered by Santosh Kumar
Solution #5
Instead of env(APP ENV), use Config::get(‘app.env’); because you’ll receive the same error and that’s not good for a live website.
Go to your config app and find this: If you want to add custom variables from your ENV, go to 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 after “‘env’ => env(‘APP ENV’, ‘production’),” for example:
/*
|--------------------------------------------------------------------------
| 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'),
The “key” variable can be named as follows:
\Config::get('app.key');
You must use config:cache to reset the cache whenever you add a new variable to the app env, such as “key.”
Answered by DIGITALCRIMINAL
Post is based on https://stackoverflow.com/questions/43243732/laravel-5-env-always-returns-null