首頁 >資料庫 >mysql教程 >如何在 Laravel 中使用多個資料庫?

如何在 Laravel 中使用多個資料庫?

Patricia Arquette
Patricia Arquette原創
2024-12-21 20:10:56467瀏覽

How Can I Use Multiple Databases in Laravel?

在 Laravel 中使用多個資料庫

Laravel 的多功能性擴展到同時處理多個資料庫。請依照下列步驟完成此動作:

定義連線

使用.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)

class ModelName extends Model { // extend changed

    protected $connection = 'pgsql';

}
在您的中設定 $connection 變數模型:

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

交易模式
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;
}

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;
    }
}
您也可以在運作時指定連線:

關係注意事項

跨資料庫建立關係是可能的,但需要小心處理。它依賴於所使用的特定資料庫和設定。

以上是如何在 Laravel 中使用多個資料庫?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn