Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

290
Visualizações
Laravel DB connection not refreshing after changing the config

This is my database connection which I have specified.

'mysql_test' => [
          'driver' => 'mysql',
          'host' => env('DB_HOST', '127.0.0.1'),
          'port' => env('DB_PORT', '3306'),
          'database'=> '',
          'username' => env('DB_USERNAME', 'forge'),
          'password' => env('DB_PASSWORD', ''),
          'unix_socket' => env('DB_SOCKET', ''),
          'charset' => 'utf8mb4',
          'collation' => 'utf8mb4_unicode_ci',
          'prefix' => '',
          'prefix_indexes' => true,
          'strict' => true,
          'engine' => null,
      ],

Now I am trying to change the database value from the code and then connection to the database like this

 for( $i = 1; $i <=4 ; $i++)
    {
      $db='test_'.$i;
    DB::connection('mysql_test')->statement('CREATE DATABASE '.$db.';' );

   Config::set('database.connections.mysql_test.database' , $db);
    error_log(config('database.connections.mysql_test.database'));
    DB::reconnect('mysql_test');
    error_log(DB::connection('mysql_test')->getDatabaseName());

  }

config value is getting changed as on the first error_log, I am getting my changed config value. But when I connect back again to the same connection with the updated config, I am not getting my database name. Connection is not getting associated to any database.

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

You can try to purge the connection so it disconnects and removes it from the cache on the Database Manager:

...

Config::set('database.connections.mysql_test.database', $db);

DB::purge('mysql_test');

dump(DB::connection('mysql_test')->getDatabaseName());
over 4 years ago · Santiago Trujillo Relatório

0

You need to reconnect the db after the config value is changed

DB::reconnect("mysql_test");

over 4 years ago · Santiago Trujillo Relatório

0

DB::Purge() fixed it for me. It looks like this was needed after Laravel 8.x upgrade. Before the Laravel tasks did switch database connections when needed. After some update they did no longer.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda