I am struggling to figure out how I can make a database query inside of a Laravel config file.
I currently use:
I tried out @jfadich's answer and found it wasn't quite solving the issue for me (laravel 5.4). I guess this is probably due to a different laravel version, but modifying his instructions I was able to get something working. By all means if I'm a bit wide of the mark in my approach please let me know, and I'll amend my answer.
So, I worked in the AppServiceProvicer itself, but you could also create a new one as previously suggested.
The line $this->app['config']->put('database.connections.mysql.version', $result->version);
was giving me an error (Call to undefined method Illuminate\Config\Repository::put())
I had a rummage in the Illuminate\Config\Repository file to see what methods it had defined and found a set()
method instead, which seems to work. I set my file up as so:
//in AppServiceProvider.php
use Illuminate\Contracts\Config\Repository; //allow setting of app config values
public function boot(Repository $appConfig)
{
$config = CustomConfig::first(); //get the values you want to use
$appConfig->set('configfile.username', $config->demo_username);
$appConfig->set('configfile.account', $config->demo_account);
}
I also came across the useful method all()
- a dd of $appConfig->all()
gave an easy way to check that the values had been set as intended.
Many thanks to @jfadich for putting me on the right path :)