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

$settings = $cache->remember('settings', 60, function() use ($settings)
    {
        return $settings->pluck('value', 'name')->all();
    });

config()->set('settings', $settings);

settings table:

enter image description here

我能够像这样回应表格中的值:

{{ config('settings.sitename') }}  //returns Awesome Images

这在App\Http\Controllers中的任何Blade 文件或控制器上都工作得很好

Problem:

我试着像下面这样将值回复到App\config\mail.php:

'driver' => config('settings.maildriver'),
'host' => config('settings.mailhost'),

But I'm getting this error:

Missing argument 1 for Illuminate\Support\Manager::createDriver()

Update:

我已经创建了一个新的服务Provider MailServiceProvider来覆盖邮件中的设置.比如:

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Config;

class MailServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {
        //
    }

    /**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    {
        Config::set('mail.driver', config('settings.maildriver'));
        Config::set('mail.host', config('settings.mailhost'));
        Config::set('mail.port', config('settings.mailport'));
        Config::set('mail.encryption', config('settings.mailencryption'));
        Config::set('mail.username', config('settings.mailusername'));
        Config::set('mail.password', config('settings.mailpassword'));

    }
}

But still I am getting the same error!!

Is there any way to override default mail configuration (in app/config/mail.php) on-the-fly (e.g. configuration is stored in database) before swiftmailer transport is created?

推荐答案

Struggled for 3 days with this issue finally I figured out a way to solve it.

首先,我创建了一个表mails,并用我的值填充它. 然后我创建了一个提供者MailConfigServiceProvider.php

<?php

namespace App\Providers;

use Config;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\ServiceProvider;

class MailConfigServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {
        //
    }

    /**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    {
        if (\Schema::hasTable('mails')) {
            $mail = DB::table('mails')->first();
            if ($mail) //checking if table is not empty
            {
                $config = array(
                    'driver'     => $mail->driver,
                    'host'       => $mail->host,
                    'port'       => $mail->port,
                    'from'       => array('address' => $mail->from_address, 'name' => $mail->from_name),
                    'encryption' => $mail->encryption,
                    'username'   => $mail->username,
                    'password'   => $mail->password,
                    'sendmail'   => '/usr/sbin/sendmail -bs',
                    'pretend'    => false,
                );
                Config::set('mail', $config);
            }
        }
    }
}

And then registered it in the config\app.php

App\Providers\MailConfigServiceProvider::class,

Laravel相关问答推荐

@vite指令在使用laravel vite构建后导致错误

Laravel 5 如何配置 Queue 数据库驱动程序以连接到非默认数据库?

仅针对字母的 laravel 验证规则

验证规则 required_if 与其他条件(Laravel 5.4)

使用 Eloquent (Laravel) 在 Group By 之前排序

Laravel transformer vs resource

laravel:如何在 Eloquent Query 中使用 LOWERCASE 函数?

Laravel 4,如何测试复选框是否被选中?

用 laravel 表单确认删除?

Laravel 框架类在 PHPUnit 数据提供程序中不可用

如何比较laravel中的两个加密(bcrypt)密码

Laravel timestamps() 不会创建 CURRENT_TIMESTAMP

导入文件时如何跳过第一行

Laravel - 自定义时间戳列名称

如何处理 laravel 5 中的私有图像?

如何在本地 Laravel Homestead 站点上获取 https 证书

如何仅在Blade模板存在时才包含它?

Grammar::parameterize() 必须是数组类型

Mac OS X 需要 Mcrypt PHP 扩展

Laravel 验证用户名不允许有空格