Chris
Chris

Reputation: 3486

How to make database query inside a config file?

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

I currently use:

<?php
// config/database.php

$results = \Illuminate\Support\Facades\DB::select( \Illuminate\Support\Facades\DB::raw("select version()") );
$mysql_version =  $results[0]->{'version()'};
dd($mysql_version);

return [

...

But the error that I receive is that:

RuntimeException in Facade.php line 218:
A facade root has not been set.
in Facade.php line 218
at Facade::__callStatic('raw', array('select version()')) in database.php line 2
at require('/Users/test/code/clooud/config/database.php') in LoadConfiguration.php line 70
at LoadConfiguration->loadConfigurationFiles(object(Application), object(Repository)) in LoadConfiguration.php line 39
at LoadConfiguration->bootstrap(object(Application)) in Application.php line 208
at Application->bootstrapWith(array('Illuminate\\Foundation\\Bootstrap\\LoadEnvironmentVariables', 'Illuminate\\Foundation\\Bootstrap\\LoadConfiguration', 'Illuminate\\Foundation\\Bootstrap\\HandleExceptions', 'Illuminate\\Foundation\\Bootstrap\\RegisterFacades', 'Illuminate\\Foundation\\Bootstrap\\RegisterProviders', 'Illuminate\\Foundation\\Bootstrap\\BootProviders')) in Kernel.php line 160
at Kernel->bootstrap() in Kernel.php line 144
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 116
at Kernel->handle(object(Request)) in index.php line 57
at require('/Users/test/code/clooud/public/index.php') in server.php line 128

How can I make sure that this query results in an output and resolve this error?

Thanks for any help!

Upvotes: 2

Views: 4674

Answers (3)

Roxy Walsh
Roxy Walsh

Reputation: 679

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 :)

Upvotes: 2

jfadich
jfadich

Reputation: 6348

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.

Upvotes: 6

vishva8kumara
vishva8kumara

Reputation: 355

You have a raw SQL query, and you are inside the config file. The config file has to be parsed before Laravel connects to the database.

You can simply connect to the database in pure php and do the query.

Connecting and querying a database in pure php is explained here: https://www.w3schools.com/php/php_mysql_connect.asp

$conn = mysqli_connect($servername, $username, $password);
$result = $conn->query('select version() as version');
$row = $result->fetch_assoc();
echo $row["version"];
$conn->close();

This should do it.

Upvotes: 1

Related Questions