Solve: Laravel env() Variable Always Returns Null Value

Published: Jun 12, 2020 - Updated: Mar 16, 2022

The problem mainly occurs at the time of moving our project one server to another server. I’m writing about some possible solutions.

Solution 1

Make sure you have .env file at the root directory of your Laravel project. If there is no .env file, the rename .env.example to .env.

Solution 2

Sometimes it occurs for cache. You can clear configuration cache using following commands:

php artisan config:clear
php artisan optimize
php artisan config:cache

Solution 3

If the above 2 solutions didn’t work, you run these additional commands.

These commands will clear cache, compiled views and routes cache:

php artisan cache:clear
php artisan view:clear
php artisan route:clear

These will clear the current compiled files, update the classes it needs and then write them back out so you don’t have to do it again:

php artisan clear-compiled
composer dump-autoload
php artisan optimize
That’s it. Thank you. ?