Laravel-friendly modification to the. env configuration file

Source: Internet
Author: User
Do Laravel-friendly modifications to the. env configuration file need to be modified by modifying the code each time ?. Can the env file be configured in the background?
Actually, it is easy to implement. add the following functions to your project.

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 ~

Welcome to join the group for discussion: 365969825

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.