mongodb / laravel-mongodb

A MongoDB based Eloquent model and Query builder for Laravel (Moloquent)
https://www.mongodb.com/docs/drivers/php/laravel-mongodb/
MIT License
7.02k stars 1.43k forks source link

Laravel Connection Purge and Reconnect not working #2282

Open blpraveen opened 3 years ago

blpraveen commented 3 years ago

I tried to reconnect to different db connection and run migration it is still running migration with default connection

Steps to reproduce

    DB::purge('tenant');
    DB::reconnect('tenant');
    Schema::connection('tenant')->getConnection()->reconnect();

Expected behaviour

It should connect to new connection and Run the migration files

Actual behaviour

It is running migration in the default connection

24bulut commented 1 year ago
 public static function connectMongo($userId = null)
{

    //Modellerin içerisinde zaten connection tanımlı buradaki temel olay dbname değişikliği

    if (\Illuminate\Support\Facades\Config::get('database.connections.mongodb.database') != 'user_id_' . $userId){
       DB::purge('mongodb');
        \Illuminate\Support\Facades\Config::set('database.connections.mongodb.database', 'user_id_' . $userId);
       DB::reconnect('mongodb');
    }

}