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

前端 未结 14 1042
忘掉有多难
忘掉有多难 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:36

    Tailor Otwell generate the laravel application key and set its value using this code (code modified for example purpose):

    $escaped = preg_quote('='.config('broadcasting.default'), '/');
    
    file_put_contents(app()->environmentFilePath(), preg_replace("/^BROADCAST_DRIVER{$escaped}/m", 'BROADCAST_DRIVER='.'pusher',
            file_get_contents(app()->environmentFilePath())
    ));
    

    You can find the code in the key generation class: Illuminate\Foundation\Console\KeyGenerateCommand

提交回复
热议问题