How to set .env values in laravel programmatically on the fly

前端 未结 14 1018
忘掉有多难
忘掉有多难 2020-12-01 09:05

I have a custom CMS that I am writing from scratch in Laravel and want to set env values i.e. database details, mailer details, general configuration, etc from

14条回答
  •  囚心锁ツ
    2020-12-01 09:33

    Watch out! Not all variables in the laravel .env are stored in the config environment. To overwrite real .env content use simply:

    putenv ("CUSTOM_VARIABLE=hero");

    To read as usual, env('CUSTOM_VARIABLE') or env('CUSTOM_VARIABLE', 'devault')

    NOTE: Depending on which part of your app uses the env setting, you may need to set the variable early by placing it into your index.php or bootstrap.php file. Setting it in your app service provider may be too late for some packages/uses of the env settings.

提交回复
热议问题