So verwenden Sie mehrere Datenbanken in Laravel
P粉919464207
P粉919464207 2023-10-13 09:54:58
0
2
633

Ich möchte mehrere Datenbanken in meinem System kombinieren. Meistens handelt es sich bei der Datenbank um MySQL; in Zukunft kann es jedoch anders sein, d. h. der Administrator kann Berichte wie diesen erstellen, was die Quelle für die „Verwendung heterogener Datenbanksysteme“ ist. Meine Frage lautet also:

Bietet Laravel eine Fassade

, um mit dieser Situation umzugehen? Oder verfügt ein anderes Framework über Funktionen, die das Problem besser lösen?

P粉919464207
P粉919464207

Antworte allen(2)
P粉957661544

在 Laravel 5.1 中,您指定连接:

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

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

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

P粉340264283

来自 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

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

在模型中设置$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)
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage