Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Laravel env() returns null even though the value is in .env

Errorenv('MY_KEY') returns null

Short 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.

  • Applies to Laravel 12, PHP 8.4
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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.php
  2. 2Match what you see, then jump to its fix

    1. 1Everything works locally, but returns null after deploy, where config:cache runsenv() is called in app code while the config is cachedFix
    2. 2You 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. 3Only keys whose value contains a space come back wrong or emptyA value with spaces is not wrapped in double quotesFix
    4. 4APP_ENV is set outside .env, and the keys you expect live in .envA different environment file is loadedFix

What does the error look like?

  • env('MY_KEY') returns null
  • config('services.my_api.key') returns null
  • Works 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=*.php

2Fix

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 services

Confirmed by laravel.com

Did this fix it?

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

2Fix

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:cache

3Verify

Read the key back through config().

php artisan config:show app

Confirmed by laravel.com

Did this fix it?

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

2Fix

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 app

Confirmed by laravel.com

Did this fix it?

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=environment

3Verify

The about output shows the environment you expect, and the value now resolves.

php artisan config:show app

Confirmed by laravel.com

Did this fix it?

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 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 fix

Back 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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.