Using the laravel framework to develop PHP programs, it is vital to configure the framework's. env file, which requires the configuration of databases, database users, and caches. The following article describes how Laravel can modify the. env configuration file. For more information, see. Using the laravel framework to develop PHP programs, it is vital to configure the framework's. env file, which requires the configuration of databases, database users, and caches. The following article describes how Laravel can modify the. env configuration file. For more information, see.
Preface
Do I have to modify the. env code every time ?. Can the env file be configured in the background? Actually, it is easy to implement. add the following functions to your project.
Sample code
function modifyEnv(array $data) { $envPath = base_path() . DIRECTORY_SEPARATOR . '.env'; $contentArray = collect(file($envPath, FILE_IGNORE_NEW_LINES)); $contentArray->transform(function ($item) use ($data){ foreach ($data as $key => $value){ if(str_contains($item, $key)){ return $key . '=' . $value; } } return $item; }); $content = implode($contentArray->toArray(), "\n"); \File::put($envPath, $content);}
Use
// You can update any value you want key => value $ data = ['app _ env' => 'Your _ environment ', 'app _ key' => 'Your _ key', 'app _ debug' => 'trueorfalse', 'DB _ database' => 'test ', 'db _ username' => 'test', 'DB _ password' => 'test', 'DB _ host' => 'localhost ', 'cache _ DRIVER '=> 'File', 'session _ DRIVER' => 'File',]; // or $ data = ['DB _ host' => '127. 0.0.1 ',]; // use the function to update modifyEnv ($ data );
Is it easy? in this way, you can configure the Laravel. env file in a graphical way in the background ~
The preceding section describes how to modify the. env configuration file of the Laravel framework. For more information, see other related articles in the first PHP community!