首先在config/database.php文件中声明连接:

return array(
    'default' => 'mysql',
    'connections' => array(
        # Our primary database connection
        'mysql' => array(
            'driver'    => 'mysql',
            'host'      => 'host1',
            'database'  => 'database1',
            'username'  => 'user1',
            'password'  => 'pass1'
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
        ),

        # Our secondary database connection
        'mysql2' => array(
            'driver'    => 'mysql',
            'host'      => 'host2',
            'database'  => 'database2',
            'username'  => 'user2',
            'password'  => 'pass2'
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
        ),
    ),
);

大致的意思是在查询的时候,指明你前面声明的连接名称:

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

或者在eloquent的model里面指明你要使用的连接:

<?php
class SomeModel extends Eloquent {
    protected $connection = 'mysql2';
}

results matching ""

    No results matching ""