gpt4 book ai didi

database - Schema Facade 中的 DB::reconnect()?

转载 作者:搜寻专家 更新时间:2023-10-30 23:36:39 36 4
gpt4 key购买 nike

我正在开发一个多数据库系统。系统会集成客户端本地的mssql服务器。

我会将凭证信息存储在我的主数据库中。我想将一些表迁移到我的客户本地数据库。

如您所知,我们可以选择如下连接。

Schema::connection('sqlsrv')->create('products', function (Blueprint $table) {

});

但我必须动态地使用它。

此连接从 config/database.php 读取

'connections' => [

'sqlite' => [
'driver' => 'sqlite',
'database' => env('DB_DATABASE', database_path('database.sqlite')),
'prefix' => '',
],

'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],

'sqlsrv' => [
'driver' => 'sqlsrv',
'host' => env('SQLSRV_HOST', 'localhost'),
'database' => env('SQLSRV_DATABASE', 'forge'),
'username' => env('SQLSRV_USERNAME', 'forge'),
'password' => env('SQLSRV_PASSWORD', ''),
'collation' => 'Turkish_CI_AS',
'charset' => 'utf8',
'prefix' => env('SQLSRV_PREFIX', ''),
],

'pgsql' => [
'driver' => 'pgsql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '5432'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public',
],

],

我在使用 DB Facade 时做到了。

public function getConnection($connection_id)
{
/** Bağlantı Bilgilerini Çeker */
$connection = Connection::find($connection_id);

try
{
/** Bağlantı Ayarları */
config()->set('database.connections.sqlsrv.host', $connection->host);
config()->set('database.connections.sqlsrv.database', $connection->db);
config()->set('database.connections.sqlsrv.username', $connection->username);
config()->set('database.connections.sqlsrv.password', $connection->password);
config()->set('database.connections.sqlsrv.charset', $connection->charset);
config()->set('database.connections.sqlsrv.collation', $connection->collation);
config()->set('database.connections.sqlsrv.prefix', $connection->prefix);

/** Ayarlar ile yeniden bağlanma */
return DB::reconnect($connection->driver);
}catch (\Exception $e)
{
throw new \Exception($e);
}
}

Schema Facade 中是否有类似 DB::reconnect() 的方法或解决方案?

最佳答案

这就是我最终做的事情。希望对您有所帮助。

    // Erase the tenant connection, thus making Laravel
// get the default values all over again.
DB::purge('tenant');

// Make sure to use the database name we want to establish a connection.
Config::set('database.connections.tenant.host', $company->database->main_server);
Config::set('database.connections.tenant.database', $company->mysql_database);
Config::set('database.connections.tenant.username', $company->mysql_username);
Config::set('database.connections.tenant.password', $company->mysql_password);

// Rearrange the connection data
DB::reconnect('tenant');

// Ping the database. This will thrown an exception in case
// the database does not exists.
Schema::connection('tenant')->getConnection()->reconnect();

我注意到,如果您不执行 DB::purge,Schema 将无法获取任何连接更改。

另一方面,如果您执行了清除操作,但数据库不存在,那么在您实际尝试对数据库连接执行某些操作之前,您不会得到异常。最后一行中的 reconnect() 将触发它。

关于database - Schema Facade 中的 DB::reconnect()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41363377/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com