Laravel env() returns null even though the value is in .env
env('MY_KEY') returns nullShort answer
Once you run php artisan config:cache, Laravel stops loading .env, so env() only sees real system variables. Call env() only inside files in the config folder, and read the value everywhere else with config(). Then run php artisan config:clear and cache again so the new config is built.
Fix it fast
1Find out which cause you have
Find out whether the configuration is cached on this machine. A cached config is the cause in most reports.
ls -l bootstrap/cache/config.php2Match what you see, then jump to its fix
- 1
Everything works locally, but returns null after deploy, where config:cache runsenv() is called in app code while the config is cachedFix - 2
You edited .env, but the app still shows the old value or null for the new keyThe cached config is older than your .env changeFix - 3
Only keys whose value contains a space come back wrong or emptyA value with spaces is not wrapped in double quotesFix - 4
APP_ENV is set outside .env, and the keys you expect live in .envA different environment file is loadedFix
- 1
What does the error look like?
env('MY_KEY') returns nullconfig('services.my_api.key') returns nullWorks locally, null in production
Cause 1 of 4
env() is called in app code while the config is cached
1Check
The docs say that once config is cached the .env file is not loaded, and env() returns only external, system-level variables. List every env() call outside the config folder.
grep -rn "env(" app/ routes/ resources/ database/ --include=*.php2Fix
Move the env() call into a config file, then read it through config() in your code.
// config/services.php
'my_api' => [
'key' => env('MY_API_KEY'),
],
// anywhere in the app
$key = config('services.my_api.key');3Verify
Rebuild the cache and read the value through config().
php artisan config:cache
php artisan config:show servicesConfirmed by laravel.com
Cause 2 of 4
The cached config is older than your .env change
1Check
A cached config is a snapshot. A key added to .env afterwards is not in it, and the file is not read again.
ls -l bootstrap/cache/config.php .env2Fix
Clear the cached configuration, or clear it and build it again so the new value is baked in.
php artisan config:clear
php artisan config:cache3Verify
Read the key back through config().
php artisan config:show appConfirmed by laravel.com
Cause 3 of 4
A value with spaces is not wrapped in double quotes
1Check
The docs say a value that contains spaces has to be enclosed in double quotes. Look at the line in .env.
grep -n "APP_NAME" .env2Fix
Wrap the value in double quotes.
APP_NAME="My Application"3Verify
Clear the config and print the value.
php artisan config:clear
php artisan config:show appConfirmed by laravel.com
Cause 4 of 4
A different environment file is loaded
1Check
If APP_ENV is supplied externally or with --env, Laravel loads .env.[APP_ENV] when that file exists, and then .env is not used.
echo $APP_ENV
ls -a | grep "^\.env"2Fix
Put the key in the file that matches APP_ENV, or remove the file that overrides .env.
php artisan about --only=environment3Verify
The about output shows the environment you expect, and the value now resolves.
php artisan config:show appConfirmed by laravel.com
How do I stop it coming back?
- Use env() only in config files. Everywhere else, call config().
- Run php artisan config:cache as the last step of every deploy, after .env is in place.
- Skip config:cache on your development machine.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Back end
Composer says the gd extension is missing on a Laravel project
composer install or composer update stops on a package that requires ext-gd
Composer refuses to install a package that requires ext-gd when the PHP binary running Composer has no GD extension.
4 causes, each with a fixBack end
composer require maatwebsite/excel fails to install
Composer says the package needs an older PHP than the one installed
Composer refuses maatwebsite/excel when the PHP version or a PHP extension does not match what the package and PhpSpreadsheet need.
3 causes, each with a fixBack end
composer install says the lock file is not compatible and asks for update
Your lock file does not contain a compatible set of packages. Please run composer update.
composer install checked the locked packages against this machine's PHP and extensions and found a conflict.
3 causes, each with a fixBack end
composer require laravel/horizon fails: ext-pcntl is missing
laravel/horizon v1.1.0 requires ext-pcntl * -> the requested PHP extension pcntl is missing from your system
Horizon needs the pcntl extension, and Composer refuses to install it where PHP lacks that extension.
3 causes, each with a fixBack end
composer require laravel/passport stops with 'requirements could not be resolved'
... but these were not loaded, likely because it conflicts with another require.
Composer found no Passport release that fits your project at once.
4 causes, each with a fixBack end
Composer says the root composer.json requires a PHP version your PHP does not satisfy
Your requirements could not be resolved to an installable set of packages.
Composer compares the php constraint in your composer.json, or in a package, with the PHP that runs Composer.
3 causes, each with a fix