Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

SergioGregorutti's avatar

Accessing Configuration Values on "config/database.php"

Hi all,

Im using the Configuration Values for setting different database name depending on the domain.

And then, I need to use the variable on the file "config/database.php". But when I call the variable I get this error:

"Deprecated: Non-static method Illuminate\Config\Repository::get() should not be called statically, assuming $this from incompatible context in /home/vagrant/Code/argemundo/config/database.php on line 60"

This is the code:

...
'database'  => Config::get('world.name'),
...

How can I use the variable on that file? Or what is the best approach to handle that?

Im seting the variable on the service provider "AppServiceProvider".

Thanks in advance!

0 likes
16 replies
usman's avatar

@SergioGregorutti I think you are using the Illuminate\Config\Repository namespace, use the Facade instead:

use Config:

....
..........
Config::get('world.name');

Usman.

SergioGregorutti's avatar

@usman I tried and I get this error:

"Fatal error: Class 'Config' not found in /home/vagrant/Code/argemundo/config/database.php on line 60"

I put the "use Config;" on "app/database.php" just before the "return [".

usman's avatar

@SergioGregorutti , I missed the point that you are using Config inside the database.php. It is not possible to use Config facade inside the Configuration files, because internally laravel registers the facades after the configurations. You can use the config helper though:

config('world.name');
SergioGregorutti's avatar

Im sorry, this is not resolved yet.

When I call the config('value') on "app/database.php" I think the variable is not defined yet, so I get an empty result.

Where I need to define the Configuration Value to be available in this file?

Currently, Im defining the variable on the boot() method of "AppServiceProvider.php".

usman's avatar

@SergioGregorutti Please unmark the answer. This cannot be done apparently, I have been waiting for you to say this. Can you paste your code. I may able to suggest something else.

SergioGregorutti's avatar

@usman Sorry, but I can´t unmark the answer. I don´t know if its possible.

This is the code:

AppServiceProvider (Is working fine):

...
use Illuminate\HTTP\Request;
use Illuminate\Config\Repository as Config;
...
public function boot(Request $request, Config $config)
{
    // Set global variables for all Worlds.
    $domain = $request->root();
    switch($domain) {
        case "http://local.argemundo.com":
            $config->set('world.name', 'Mundo Hurlingham');
            $config->set('world.city', 'Hurlingham');
            $config->set('world.id', 'hurlingham');
            break;
    }
}
...

app/database.php (The "world.id" is empty):

...
'database'  => 'am_'.config('world.id'),
...
SergioGregorutti's avatar

By the way, if I use "config('world.id')" on a view I can see the correct value. The problem is in "app/database.php".

usman's avatar

@SergioGregorutti of course you will get the value inside the view, config files are loaded before the AppServiceProvider or any other ServiceProvider so the value is not being set in the database.php.

SergioGregorutti's avatar

@usman Got it :)

So, now I´m scratching my head thinking on how can I use different databases depending on the domain hehe

usman's avatar
usman
Best Answer
Level 27

@SergioGregorutti did you try:

    switch($domain) {
        case "http://local.argemundo.com":
    ............
    //mysql for instance.
            $config->set('database.connections.mysql.database', 'am_hurlingham');
            break;
    }

This would work, not sure if this suits your needs.

SergioGregorutti's avatar

@usman Whoops! It's 90% resolved...

When I run for example "php artisan migrate:reset" the "config('world.id')" is empty. I can´t believe it... hehe

I don´t have any idea of how can I set the variable before "artisan" is ready...

Well, I don´t have even a domain when I run the artisan.

SergioGregorutti's avatar

@usman You're right. There's no way to set the database with this logic on Artisan. Anyway, I just figure out setting the database name on the .dev file. This is just for artisan. So, if I need to run migrations on different databases I just need to change the database name on the .env file

Thanks!

Please or to participate in this conversation.