Chitrasen's avatar

how to make changes in .env file without reloading server

I am using a form from front end to change my .env file variable values, after changing the values, changes are being saved in my env file, but its not reflecting on my laravel application, after restarting my server its reflecting on my website, but i cant do this if its in live server, I tried PHP artisan config:clear and PHP artisan cache:clear but none of its working. Can anyone please tell me how to do this without restarting server.

I am trying to change these values in .env HTTP_HOST=http:// SFTP_HOST=10.10.10.64

0 likes
12 replies
ChristophHarms's avatar

The .env file is read during bootstrapping on each request (see here and here).

So a change to your .env file should be accounted for as early as during the next request, concerning web requests. You only have to restart your long-running processes like queue workers and such, wich you can do one-by-one.

If a change to your .env is not reflected on the next request, it has to be some caching issue, since the code I linked to clearly shows that it should be.

EDIT: In this thread on laracasts someone had a similar problem and solved it by deleting the vendor folder and doing composer install "to make sure nothing was broke in my autoloading".

Nakov's avatar
Nakov
Best Answer
Level 73

And just to add to @christophharms reply, you should use env variables though the config files, so in case you do that already you just need to clear the config cache in case you cached it.. That can be done using

php artisan config:clear

This should be done in case you introduced a new variable, not when changing an existing one.

2 likes
Chitrasen's avatar

nakov its not working, i am calling env variables form config after editing env variable values from frontend, variables got changed in env but not reflecting in app.php file, please tell me how to do it

Chitrasen's avatar

I did php artisan config:cache and config:clear but both commands not worked

Nakov's avatar

@chitrasen I asked for a usage of an .env variable in your code..

so in your .env you have:

SOME_VARIABLE=testing

then in a config file, for example config/services.php you have this:


'myservice' => [
    'test' => env('SOME_VARIABLE')
]   

And in your code you use it like this:

config('services.myservice.test')

Is this your flow?

Chitrasen's avatar

'HTTP_HOST' => env('HTTP_HOST'), 'SFTP_HOST' => env('SFTP_HOST'),

i kept this code in my app.php file to read those values from env, and in my controller i am retriving as shown below $http_host = Config::get('app.HTTP_HOST'); $sftp_host = Config::get('app.SFTP_HOST');

after doing changes in env file, its changing in .env file bit not reflecting in app.php

Nakov's avatar

It might be a permission problem so that the config:clear won't clear your cached file. You can try to find the cached file in bootstrap/cache/config.php and remove that one manually if that's the case.

Chitrasen's avatar

thnk you nakov your solution helped me temporarily, need to ix it permanently, thnk u very much for the help

ChristophAust's avatar

Are you jobs being executed by workers from a queue? In that case you need to call

php artisan queue:restart

for changes to take effect. Other than that I think env could be the wrong place for what you try to do.

2 likes

Please or to participate in this conversation.