30

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?

miken32
  • 42,008
  • 16
  • 111
  • 154
Steve
  • 1,622
  • 5
  • 21
  • 39
  • Does this answer your question? [Laravel 5 - env() always returns null](https://stackoverflow.com/questions/43243732/laravel-5-env-always-returns-null) – miken32 Jul 14 '23 at 17:33

11 Answers11

47

Check APP_ENV in your .env file, if it's value is correct (production in OP's case) then yes, Laravel is caching it.

Once cached, the .env file is ignored, and env(...) function returns null.

You should run these commands after changing configs:

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

Note that "php artisan config:cache" may be enough, but changing APP_ENV could add or remove routes or something like that (is your App, you know better).

Top-Master
  • 7,611
  • 5
  • 39
  • 71
MoPo
  • 1,461
  • 9
  • 7
  • 3
    For laravel 5.6 and up, `php artisan optimize` is removed. Its a good idea just update this anwser, @MoPo. Ref: https://laravel-news.com/laravel-5-6-removes-artisan-optimize – Emerson Rocha Jan 15 '19 at 05:32
  • 1
    @EmersonRocha `optimize` and `optimize:clear` definitely still exist in Laravel 8. – Hashim Aziz May 24 '23 at 20:14
33

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.

Community
  • 1
  • 1
Sanzeeb Aryal
  • 4,358
  • 3
  • 20
  • 43
18

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

laravel >= 7

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

in all Laravel versions after 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!

laravel 6 | 5 | 4

you should run all these commands

php artisan cache:clear
php artisan view:clear
php artisan route:clear
php artisan clear-compiled
php artisan config:clear

and you will get these messages one by one:

Application cache cleared!
Compiled views cleared!
Route cache cleared!
Compiled services and packages files removed!
Configuration cache cleared!
miken32
  • 42,008
  • 16
  • 111
  • 154
Raskul
  • 1,674
  • 10
  • 26
8

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.

Tobias Wilfert
  • 919
  • 3
  • 14
  • 26
tanderson
  • 136
  • 1
  • 4
7

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

Jasbin Karki
  • 574
  • 7
  • 15
3
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

Maulik Shah
  • 421
  • 2
  • 10
3

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.

Abilogos
  • 4,777
  • 2
  • 19
  • 39
1

From the documentation:

You should typically run the php artisan config:cache command as part of your production deployment process. The command should not be run during local development as configuration options will frequently need to be changed during the course of your application's development...

If you execute the config:cache command during your deployment process, you should be sure that you are only calling the env function from within your configuration files. Once the configuration has been cached, the .env file will not be loaded; therefore, the env function will only return external, system level environment variables.

It's therefore safest to never run config:cache or any other caching commands (such as php artisan optimize, which also caches the config) when developing locally. If you think you might have run it locally and want to rule it out as the cause of any issues you're currently having, simply run config:clear or the more powerful optimize:clear.

Hashim Aziz
  • 4,074
  • 5
  • 38
  • 68
-1

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.

Pathros
  • 10,042
  • 20
  • 90
  • 156
-1

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

ioepracas
  • 15
  • 4
  • This is useful when you don't have shell access and programmatically execution of artisan commands might be lengthy process. Just delete files and boom ! – Sagar Gautam Dec 23 '20 at 13:44
  • How could this work properly if the cache is not using the filesystem? – Nico Haase Feb 03 '23 at 12:38
-1

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!

SpinyMan
  • 436
  • 5
  • 9