如何在其他文件中使用 serviceProvider laravel

How to use serviceProvider into other file laravel

我正在尝试将我的新 serviceProvider 用于 mail.php 因为我需要从数据库中获取值而不是 .env 我可以在这个网站上展示一个解决方案,一个人创建了一个 serviceProvider 并获得所有他需要的数据。

我的问题是,如何将此提供程序用于 mail.php?

我的提供商:

public function register()
{
    if (\Schema::hasTable('app_settings')) {
        $mail = DB::table('app_settings')->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);
        }
    }
}

/**
 * Bootstrap services.
 *
 * @return void
 */
public function boot()
{
    //
}

mail.php

例如:'driver' => env('MAIL_DRIVER', 'smtp'),

如何将提供者的驱动程序调用到驱动程序中mail.php

感谢帮助,打赏

你做不到,也不是个好主意。

最简单的方法是在加载所有内容后覆盖您的配置。

您可以使用您的 AppServiceProdiver.php 文件,并在 boot() 函数中:

public function boot()
{
   // adapt of course...
   $yourConfig = \DB::select('SELECT * FROM your_config_table');

   config()->set([
       'app.mail.driver' => $yourConfig[0]->value
   ]);
}