How to make database query inside a config file?

后端 未结 3 1687
时光说笑
时光说笑 2020-12-22 02:08

I am struggling to figure out how I can make a database query inside of a Laravel config file.

I currently use:



        
3条回答
  •  再見小時候
    2020-12-22 02:36

    Leave a default or null value in your config/database.php file. Create a new service provider (either with the artisan command or manually)

    php artisan make:provider DatabaseConfigProvider
    

    Then add the new provider to the $providers array in your config/app.php file.

    Finally add the following code to the boot() method.

    public function boot()
    {
        $result= \DB::select('select version() as version')[0];
        $this->app['config']->put('database.connections.mysql.version', $result->version);
    }
    

    The key in the put() argument can be whatever you want.

提交回复
热议问题