The issue might be that the extend method is not being called at the right time. Try calling it in a service provider's boot method instead of directly in your code. Here's an example:
- Create a new service provider by running the following command:
php artisan make:provider DatabaseServiceProvider
- Open the newly created
DatabaseServiceProviderand add the following code to thebootmethod:
use Illuminate\Support\Facades\DB;
use Illuminate\Support\ServiceProvider;
class DatabaseServiceProvider extends ServiceProvider
{
public function boot()
{
DB::extend('connection-name', function ($config, $name) {
$config['database'] = 'new-database-name';
return new \Illuminate\Database\MySqlConnection($config, $name);
});
}
}
- Register the service provider in
config/app.php:
'providers' => [
// ...
App\Providers\DatabaseServiceProvider::class,
],
- Run your application and check the logs to see if the message is being logged.