gpt4 book ai didi

php - 如何在 Laravel 中使用多个数据库

转载 作者:IT老高 更新时间:2023-10-28 11:42:36 25 4
gpt4 key购买 nike

我想在我的系统中合并多个数据库。大多数情况下,数据库是 MySQL;但将来可能会有所不同,即管理员可以生成这样的报告,它是使用异构数据库系统的来源

所以我的问题是 Laravel 是否提供任何 Facade 来处理这种情况?或者任何其他框架有更适合问题的能力是什么?

最佳答案

测试版本(更新)

<头>
版本 已测试(是/否)
4.2 没有
5.x 是 (5.5)
6.x 没有
7.x 没有
8.x 是的 (8.4)
9.x 是的 (9.2)

定义连接

Using .env >= 5.0 (or higher)

.env

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=database1
DB_USERNAME=root
DB_PASSWORD=secret

DB_CONNECTION_SECOND=mysql
DB_HOST_SECOND=127.0.0.1
DB_PORT_SECOND=3306
DB_DATABASE_SECOND=database2
DB_USERNAME_SECOND=root
DB_PASSWORD_SECOND=secret

config/database.php

'mysql' => [
'driver' => env('DB_CONNECTION'),
'host' => env('DB_HOST'),
'port' => env('DB_PORT'),
'database' => env('DB_DATABASE'),
'username' => env('DB_USERNAME'),
'password' => env('DB_PASSWORD'),
],

'mysql2' => [
'driver' => env('DB_CONNECTION_SECOND'),
'host' => env('DB_HOST_SECOND'),
'port' => env('DB_PORT_SECOND'),
'database' => env('DB_DATABASE_SECOND'),
'username' => env('DB_USERNAME_SECOND'),
'password' => env('DB_PASSWORD_SECOND'),
],

Note: In mysql2 if DB_username and DB_password is same, then you can use env('DB_USERNAME') which is metioned in .env first few lines.

Without .env <= 4.0 (or lower)

app/config/database.php

return array(

'default' => 'mysql',

'connections' => array(

# Primary/Default database connection
'mysql' => array(
'driver' => 'mysql',
'host' => '127.0.0.1',
'database' => 'database1',
'username' => 'root',
'password' => 'secret'
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),

# Secondary database connection
'mysql2' => array(
'driver' => 'mysql',
'host' => '127.0.0.1',
'database' => 'database2',
'username' => 'root',
'password' => 'secret'
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
),
);

架构/迁移

运行 connection()方法来指定要使用的连接。

Schema::connection('mysql2')->create('some_table', function($table)
{
$table->increments('id'):
});

或者,在顶部,定义一个连接。

protected $connection = 'mysql2';

查询生成器

$users = DB::connection('mysql2')->select(...);

模型(在 Laravel >= 5.0(或更高版本)中)

设置$connection模型中的变量

class ModelName extends Model { // extend changed

protected $connection = 'mysql2';

}

Eloquent(在 Laravel <= 4.0(或更低版本)中)

设置$connection模型中的变量

class SomeModel extends Eloquent {

protected $connection = 'mysql2';

}

您也可以在运行时通过 setConnection 定义连接。方法或 on静态方法:

class SomeController extends BaseController {

public function someMethod()
{
$someModel = new SomeModel;

$someModel->setConnection('mysql2'); // non-static method

$something = $someModel->find(1);

$something = SomeModel::on('mysql2')->find(1); // static method

return $something;
}

}

Note: Be careful about attempting to build relationships with tables across databases! It is possible to do, but it can come with some caveats depending on your database and settings.


From Laravel Docs

使用多个数据库连接

当使用多个连接时,您可以访问每个 connection 通过 DB 上的连接方法 门面。 name 传递给 connection 方法应对应于您的 config/database.php 中列出的连接之一 配置文件:

$users = DB::connection('foo')->select(...);

您也可以使用 getPdo 访问原始的底层 PDO 实例。连接实例上的方法:

$pdo = DB::connection()->getPdo();

有用的链接

  1. Laravel 5 multiple database connections FROM laracasts.com
  2. Connect multiple databases in Laravel FROM tutsnare.com
  3. Multiple DB Connections in Laravel FROM fideloper.com

关于php - 如何在 Laravel 中使用多个数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31847054/

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