首页  >  问答  >  正文

Laravel中如何使用多个数据库

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

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

P粉919464207P粉919464207342 天前575

全部回复(2)我来回复

  • P粉957661544

    P粉9576615442023-10-14 00:28:20

    在 Laravel 5.1 中,您指定连接:

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

    默认,Laravel 使用默认连接。很简单,不是吗?

    在此处阅读更多信息:http://laravel.com/docs/5.1/database#访问连接

    回复
    0
  • P粉340264283

    P粉3402642832023-10-14 00:26:49

    来自 Laravel 文档:您可以访问每个连接使用多个连接时,通过 DB 外观上的连接方法。传递给连接方法的名称应与 config/database.php 配置文件中列出的连接之一相对应:

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

    定义连接

    使用 .env >= 5.0 (或更高)

    DB_CONNECTION=mysql
    DB_HOST=127.0.0.1
    DB_PORT=3306
    DB_DATABASE=mysql_database
    DB_USERNAME=root
    DB_PASSWORD=secret
    
    DB_CONNECTION_PGSQL=pgsql
    DB_HOST_PGSQL=127.0.0.1
    DB_PORT_PGSQL=5432
    DB_DATABASE_PGSQL=pgsql_database
    DB_USERNAME_PGSQL=root
    DB_PASSWORD_PGSQL=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'),
    ],
    
    'pgsql' => [
        'driver'    => env('DB_CONNECTION_PGSQL'),
        'host'      => env('DB_HOST_PGSQL'),
        'port'      => env('DB_PORT_PGSQL'),
        'database'  => env('DB_DATABASE_PGSQL'),
        'username'  => env('DB_USERNAME_PGSQL'),
        'password'  => env('DB_PASSWORD_PGSQL'),
    ],

    没有 .env <= 4.0(或更低)<= 4.0(或更低)

    app/config/database.php

    return array(
        'default' => 'mysql',
        'connections' => array(
            # Primary/Default database connection
            'mysql' => array(
                'driver'    => 'mysql',
                'host'      => '127.0.0.1',
                'database'  => 'mysql_database',
                'username'  => 'root',
                'password'  => 'secret'
                'charset'   => 'utf8',
                'collation' => 'utf8_unicode_ci',
                'prefix'    => '',
            ),
    
            # Secondary database connection
           'pgsql' => [
                'driver' => 'pgsql',
                'host' => 'localhost',
                'port' => '5432',
                'database' => 'pgsql_database',
                'username' => 'root',
                'password' => 'secret',
                'charset' => 'utf8',
                'prefix' => '',
                'schema' => 'public',
            ]
        ),
    );

    架构/迁移

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

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

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

    protected $connection = 'pgsql';

    查询生成器

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

    型号

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

    在模型中设置$connection变量

    class ModelName extends Model { // extend changed
    
        protected $connection = 'pgsql';
    
    }

    雄辩

    (在 Laravel <= 4.0(或更低)中)<= 4.0(或更低)中)

    在模型中设置$connection变量

    class SomeModel extends Eloquent {
        protected $connection = 'pgsql';
    }

    交易模式

    DB::transaction(function () {
        DB::connection('mysql')->table('users')->update(['name' => 'John']);
        DB::connection('pgsql')->table('orders')->update(['status' => 'shipped']);
    });

    DB::connection('mysql')->beginTransaction();
    try {
        DB::connection('mysql')->table('users')->update(['name' => 'John']);
        DB::connection('pgsql')->beginTransaction();
        DB::connection('pgsql')->table('orders')->update(['status' => 'shipped']);
        DB::connection('pgsql')->commit();
        DB::connection('mysql')->commit();
    } catch (\Exception $e) {
        DB::connection('mysql')->rollBack();
        DB::connection('pgsql')->rollBack();
        throw $e;
    }

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

    class SomeController extends BaseController {
        public function someMethod()
        {
            $someModel = new SomeModel;
            $someModel->setConnection('pgsql'); // non-static method
            $something = $someModel->find(1);
            $something = SomeModel::on('pgsql')->find(1); // static method
            return $something;
        }
    }

    测试版本(已更新

    版本 已测试(是/否)
    4.2
    5 是(5.5)
    6
    7
    8 是(8.4)
    9 是(9.2)

    回复
    0
  • 取消回复