May Sale! All accounts are 40% off this week.

dkage's avatar
Level 1

Why my config:cache does not work?

I'm using Laravel 8.83 in a project I'm working on for 2 whole years now. During this time the application was deployed on three different production servers, on different Hostings and using different Linux distributions, and I also develop the application on a MacOS environment.

With that for context, why my php artisan config:cache never worked? Never, not on local development Macbook, and also not a single server the config:cache works. When I use the command the all the env() calls outputs null on every php file, and all blade files. If I do a php artisan config:clear, it goes back to work as intended.

I always have to run "php artisan optimize && php artisan config:cache" on every deployment or server migration, or else everything stops working due to impossibility of reading the variables from the .env file. Middleware and webhooks dies instantly because the cache does not work. Is this a problem with the version I'm using? Am I doing something wrong here?

This is the behavior on CLI also, as this "copy and paste" from my terminal block as example:

$ php artisan tinker
Psy Shell v0.11.12 (PHP 8.0.19 — cli) by Justin Hileman
> env('APP_DEBUG');
= true

> CTRL+C

$ php artisan config:cache
  Configuration cache cleared!
  Configuration cached successfully!

$ php artisan tinker
Psy Shell v0.11.12 (PHP 8.0.19 — cli) by Justin Hileman
> env('APP_DEBUG');
= null

> CTRL+C

$ php artisan config:clear
Configuration cache cleared!

$ php artisan tinker
Psy Shell v0.11.12 (PHP 8.0.19 — cli) by Justin Hileman
> env('APP_DEBUG');
= true

As you can see, the moment I clear the cache the env() start working again.

0 likes
2 replies
LaryAI's avatar
Level 58

It's possible that the issue is related to the use of the env() function in your code. When you run php artisan config:cache, Laravel will cache your configuration files, including your .env file. This means that any calls to env() will be reading from the cached configuration files, rather than the .env file directly. If you're relying on env() to read values from your .env file, this could be causing the issue you're seeing.

To fix this, you can update your code to use the config() function instead of env(). The config() function will read values from the cached configuration files, so it will work correctly even when you've run php artisan config:cache.

For example, instead of using env('APP_DEBUG'), you could use config('app.debug'). This will read the debug value from your config/app.php file, which is where the APP_DEBUG value is stored.

If you make this change and still have issues with php artisan config:cache, you may want to try updating to the latest version of Laravel to see if that resolves the issue. You could also try clearing your cache manually by deleting the bootstrap/cache/config.php file, rather than using php artisan config:clear.

Here's an example of how you could update your code to use config() instead of env():

// Before
if (env('APP_DEBUG')) {
    // Do something
}

// After
if (config('app.debug')) {
    // Do something
}
Snapey's avatar
Snapey
Best Answer
Level 122

Easy, don't use env() helper in your code .... only use in config files.

When you cache config the env values are locked into the config helper and the env file is not even read.

1 like

Please or to participate in this conversation.