Giedrius Giedrius - 9 months ago 117
PHP Question

Laravel Artisan - reload .env variables or restart framework

I'm trying to write a command as a quickstart to build a project. The command asks for input for database details and then changes the .env file. The problem is that the command needs to do some Database queries afterwards, but the .env variables are not reloaded.

My question would be, is there a way to reload or override .env variables runtime. And if not, is there a way to call another Artisan command freshly, so framework bootstraps again?

In my command I tried doing

in my actual command, but even in the second call the variables are not reloaded.

Is there a way to achieve this without making the user manually call multiple commands?


Answer Source

Laravel uses config files which take data from .env file. So, what you can do is to override configuration values at runtime:

config(['database.default' => 'mysql']);