Dynamic mail configuration with values from database [Laravel]

后端 未结 3 1641
傲寒
傲寒 2021-02-01 07:09

I have created a service provider in my Laravel Application SettingsServiceProvider. This caches the settings table from the database.

$settings = $         


        
3条回答
  •  南旧
    南旧 (楼主)
    2021-02-01 07:55

    Maybe its usefull to somebody, but I solved it as following;

    In a ServiceProvider under the boot-method;

    $settings = Cache::remember('settings', 60, function () {
        return Setting::pluck('value', 'name')->all();
    });
    
    config()->set('settings', $settings); // optional
    
    config()->set('mail', array_merge(config('mail'), [
        'driver' => 'mailgun',
        'from' => [
            'address' => $settings['mail_from_address'],
            'name' => $settings['mail_from_name']
        ]
    ]));
    
    config()->set('services', array_merge(config('services'), [
        'mailgun' => [
            'domain' => $settings['mailgun_domain'],
            'secret' => $settings['mailgun_secret']
        ]
    ]));
    

    I used array_merge with the original config, so we only override the values we need to. Also we can use the Cache-facade in the boot-method.

提交回复
热议问题