'Always have to clear cache in .env?

I am having a cache issue. I have a project and worked on local. Now whenever i upload to server and edit the .env and config/app.php file. It is not taking the effect.

But, if i set the .env configuartions of server in local and cleared the cache using php artisan config:cache and upload it to server. It works. Should i always do this method?

So everytime i need change .env i should first change it in local and clear the cache and upload in server? Or is there any method to directly command on server.

And again, in another project. Editing .env and config/app.php file directly in server takes immediate effect. What is happening?



Solution 1:[1]

Check APP_ENV in your .env file. If it's on production then yes, laravel caching it. You should run these commands before changing configs:

php artisan cache:clear
php artisan config:clear
php artisan route:clear

And then after changes run these:

php artisan config:cache
php artisan route:cache
php artisan optimize

Solution 2:[2]

With php artisan config:cache, you are first clearing the cache, and then setting the cache. You shoud get the message:

Configuration cache cleared!

Configuration cached Successfully!

Now, if you upload to server and edit .env from there, it will not take immediate effect because of the configuration is cached.

Solution: Only clear the cache: php artisan config:clear and php artisan cache:clear. Now you can upload to server and edit .env file from server with immediate effect because the Configuration is not cached.

Solution 3:[3]

2022 answer:

I read all the answers but none of them contain optimize:clear so I want to write my answer for future users.

optimize:clear is the most powerful command to clean all the caches

in Laravel >= 7 you have this command for clearing all the caches

Command:

php artisan optimize:clear

It will clear: Compiled views, Application cache, Route cache, Configuration cache, Compiled services and packages.

It is not harmful at all. and it won't affect any single line of your codes. it just will clear all your cached files.

after running this command you will see:

Compiled views cleared!
Application cache cleared!
Route cache cleared!
Configuration cache cleared!
Compiled services and packages files removed!
Caches cleared successfully!

Solution 4:[4]

If you are using a queue driver with a supervisor, then your .env variables loaded in queue functions will be cached until you restart supervisor.
In my case, I had changed some mail env variables and was confused why clearing the cache/config was not working on the remote server whereas local was working fine until I realized my local queue driver was processing mail synchronously, whereas remote server was using the queue for sending mail.

Solution 5:[5]

I tried the following things and it worked for me

1.first turn off artisan server and make changes to env file and

2.run these commands

php artisan cache:clear
php artisan config:clear
php artisan route:clear

3.run

php artisan serve

Now it should work

Solution 6:[6]

php artisan config:clear

php artisan cache:clear 

php artisan config:cache

If you are using the config:cache command during deployment, you must make sure that you are only calling the env function from within your configuration files, and not from anywhere else in your application.

If you are calling env from within your application, it is strongly recommended you add proper configuration values to your configuration files and call env from that location instead, allowing you to convert your env calls to config calls.

Read whole thread over https://github.com/laravel/framework/issues/21727

Solution 7:[7]

Yes,

config files, view files, ... can be cache in laravel duo to optimization and speed up.

if you have changed something in config.php or .env you have to run php artisan config:cache

but i have wrote a hook which will run after any push to server (with git)

if you are using git as version control, you can add these in servers .git directory:

./.git/hooks/post-receive :

#!/bin/bash

cd ..
composer install --optimize-autoloader --no-dev
php artisan config:cache
php artisan route:cache
php artisan view:cache

exit 0

this will run after every push to server.

Solution 8:[8]

To clear cache issue you may delete all the files,folders inside cache folder which is located in bootstrap folder. Delete files and folders inside this path bootstrap/cache

Solution 9:[9]

Just adding my 2 cents and completing the answer of @tanderson, I had the same situation where first I had deployed a Laravel app with Laradock and tested email sending with Mailtrap.

Then, when I changed the .env credentials to some other SMTP email service, the email notifications were still sent to Mailtrap.

First of all, I executed inside the Laravel container (workspace) the following command:

$ php artisan config:cache

It's important to point out, as stated in this answer, that you should

use env() only in config files

Otherwise, config:cache will never work in production!

Then, just like @tanderson said, I had to reload the supervisor container. Fortunately, I didn't have to restart the container:

$ sudo docker container exec php-worker supervisorctl reread
$ sudo docker container exec php-worker supervisorctl update
$ sudo docker container exec php-worker supervisorctl restart all
$ sudo docker container exec php-worker supervisorctl
$ sudo docker container exec php-worker supervisorctl status

After that, I retried sending the email and then changes made in the .env file took effect.

Solution 10:[10]

I've got the same issue with google client library that used getenv() method. Of course my config was cached and getenv() returned false. So I added these lines of code in AppServiceProvider:

if (empty($_ENV)) { //if we use config:cache this variable always empty
    putenv('GOOGLE_APPLICATION_CREDENTIALS=' . config('google.application_credentials'));
}

And now you can call env() or getenv() methods and you will get the correct value. The same way you can add any environment variables you want.

NB: you should store env variable in the config file before!

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 MoPo
Solution 2 Community
Solution 3
Solution 4 Tobias Wilfert
Solution 5
Solution 6 Maulik Shah
Solution 7 Abilogos
Solution 8 ioepracas
Solution 9
Solution 10 SpinyMan