Laravel 多數據庫配置及查詢操作


laravel文檔好像沒有寫得很詳細

https://docs.golaravel.com/docs/5.3/database/

Using Multiple Database Connections
When using multiple connections, you may access each connection via the connection method on the DB facade. 
The name passed to the connection method should correspond to one of the connections listed in your config/database.php configuration file: $users = DB::connection('foo')->select(...); You may also access the raw, underlying PDO instance using the getPdo method on a connection instance: $pdo = DB::connection()->getPdo();

 

步驟:

1、/app/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' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'strict' => false,
            'engine' => null,
        ],
        //新增01
        'mysql_01' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST_01', 'localhost'),
            'port' => env('DB_PORT_01', '3306'),
            'database' => env('DB_DATABASE_01', 'forge'),
            'username' => env('DB_USERNAME_01', 'forge'),
            'password' => env('DB_PASSWORD_01', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'strict' => false,
            'engine' => null,
        ],
     //新增02
        'mysql_02' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST_02', 'localhost'),
            'port' => env('DB_PORT_02', '3306'),
            'database' => env('DB_DATABASE_02', 'forge'),
            'username' => env('DB_USERNAME_02', 'forge'),
            'password' => env('DB_PASSWORD_02', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'strict' => false,
            'engine' => null,
        ],

 

 

 

 

2、在.env文件 配置數據庫名稱、數據賬號密碼

第一個為默認數據庫,后面兩個為需要連接查詢的數據名稱

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=dataName
DB_USERNAME=root
DB_PASSWORD=

DB_HOST_01=127.0.0.1
DB_PORT_01=3306
DB_DATABASE_01=dataName01
DB_USERNAME_01=root
DB_PASSWORD_01=

DB_HOST_02=127.0.0.1
DB_PORT_02=3306
DB_DATABASE_02=dataName02
DB_USERNAME_02=root
DB_PASSWORD_02=

 

3、在Model中進行相關操作

// connection: mysql_01 database: dataName01
$datas01 = \DB::connection('mysql_01')->table('tableName')->select('...')->first(); // connection: mysql_02 database: dataName02
$datas02 = \DB::connection('mysql_02')->table('tableName')->select('...')->first();

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM