Background:
I tried to write a setup wizard using Laravel 5.5. Some database parameters (database name, user, password) should be written to .env file and then validated. The writing process could be done with this.
Problems I met:
After wrote posted variables into .env file successfully, I tried to reload the variables using code like env('DB_DATABASE')
. I found the variables remain unchanged. But after I refreshed the page, the variable will change to correct one. I did check the variables in .env file were already updated before I refresh the page.
What I have tried:
I tried to find a solution. Most solutions refer to use artisan config:clear
command. So I put artisan command to my controller like this:
$this->changeEnvironmentVariable('DB_DATABASE',$request->input('db_name'));
$this->changeEnvironmentVariable('DB_USERNAME',$request->input('db_user'));
$this->changeEnvironmentVariable('DB_PASSWORD',$request->input('db_password'));
Artisan::call('config:clear');
But it doesn't work although there are no warnings and errors. The env('DB_DATABASE')
still keeps the previous value.
Question:
I could work around this by validating the database information using the posted information instead of load the variables from .env file. However, I just want to know whether there is a way to write and reload .env variables in real-time.
Any comments are appreciated.