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

WarrenC's avatar

Laravel 11 ignoring changes to database.php

I have a couple of different databases I need to connect to. I do artisian config:clear after making changes to the config/database.php however if I do like dd(Config::get('database')); it just returns the stock entries and not the changes I have made in the database script.

Not sure what is going on.

0 likes
12 replies
LaryAI's avatar
Level 58

When you encounter issues with Laravel not recognizing changes in your config/database.php file, there are a few steps you can take to troubleshoot and resolve the problem:

  1. Clear Configuration Cache: Ensure that you are clearing the configuration cache correctly. Run the following command to clear the cached configuration:

    php artisan config:clear
    

    This command should remove any cached configuration files, forcing Laravel to use the updated configuration files.

  2. Check Environment Configuration: Make sure that your .env file is correctly set up and that the environment variables are being loaded properly. Sometimes, the issue might be with the environment variables not being read correctly.

  3. Clear Application Cache: In addition to clearing the configuration cache, you might also want to clear the application cache to ensure there are no other cached items affecting your configuration:

    php artisan cache:clear
    
  4. Check for Cached Config Files: If you have previously run php artisan config:cache, it creates a single cached configuration file. You should clear this cache as well:

    php artisan config:clear
    

    After clearing, avoid running php artisan config:cache until you are sure your configuration is correct.

  5. Verify Config File Changes: Double-check your config/database.php file to ensure that the changes you made are saved correctly. Sometimes, it might be a simple oversight where changes were not saved.

  6. Debugging: Use dd() or dump() to debug and ensure that the configuration is being read correctly. For example, you can place a dd() in your config/database.php to see if the file is being loaded:

    dd('Database config loaded');
    

    If you don't see this output, it indicates that the file is not being loaded as expected.

  7. Check for Environment-Specific Configurations: If you are using environment-specific configuration files (e.g., config/database.local.php), ensure that they are not overriding your main configuration file.

By following these steps, you should be able to resolve the issue with Laravel not recognizing changes to your config/database.php file. If the problem persists, consider checking for any custom logic in your application that might be affecting the configuration loading process.

Tray2's avatar

First off, why are you using multiple databases?

There is almost never an issue to use a single db for your application.

WarrenC's avatar

@Tray2 Does that really matter why? Your solutions don't require multiple databases?

If I modify config/database.php Laravel ignores those changes. If I want to specify the IP or username/password why is 11 ignoring that?

Tray2's avatar

@WarrenC Of course it matters, or I wouldn't have asked.

I'm taking a guess here, and that is that you are changing the second parameter in the env function call.

'host' => env('DB_HOST', '127.0.0.1'),

'host' => env('DB_HOST', '192.168.0.5'),

If that is what you are doing, then you are changing the default value, and not the actual parameter, which is read from the .env.

I would suggest setting up multiple connections in the database.php file instead, and configure them via the .env file.

maxxd's avatar

Are you using multiple databases at the same time or are you changing databases overall? If you're using more than one database at runtime (which is totally reasonable - I've got a couple systems that integrate with MySQL, PostgreSQL, and TSQL due to mergers) you wouldn't need to clear the cache, just make sure you specify the connection in the model or query builder.

WarrenC's avatar

@maxxd What I typically do is check the session for which database to connect to and state that in the model. However when I try to do like DB::connection('db3') it says undefined array key $database.

It is just loaded the default one in the vendor folder and ignoring the one in the config folder.

maxxd's avatar

@WarrenC It's quite possible I'm confused but it sounds like you're trying to handle load balancing manually? I'm not a database admin but I'm pretty sure there are ways to set up session management automatically so as to not lose data depending on the server invoked.

WarrenC's avatar

I have an ERP system that I am accessing. Laravel is not in the production database but in a local mySQL instance. The ERP system has different schemas (like production or training or End-Of-Month) so depending on the webpage, the user could be accessing production (live data) or the training database (copied start of each week, or manually if testing configuration changes in the system before doing so in production), or End-Of-Month to see what has changed transactionally compared to production or training schema.

This is not a stand-alone web app but needs to connect to external ERP databases, depending on the need.

Again not sure but this worked fine in 10, in 11 it ignores the file and is using the one in the vendor file.

WarrenC's avatar

And yes, I did the various clear commands and what not suggested by the AI.

WarrenC's avatar

It seems that vendor/laravel/framework/config/database.php overrides config/database.php and there are various PRs to fix that issue.

I don't see how to turn the default one off. Any ideas?

WarrenC's avatar

If I create a new connection in config/database.php and in Artisan Tinker: DB::connect('test') I get:

DB::connection('test')

WARNING Undefined array key "database" in vendor/laravel/framework/src/Illuminate/Database/Connectors/ConnectionFactory.php on line 77.

WarrenC's avatar

Ah! It merges the default with your config/database.php, and for my driver it does not require the database attribute bet set, but Laravel 11 requires it. Added 'database' => '', to the connection array and it works now.

Simple

Please or to participate in this conversation.