我们如何在Laravel中使用MySQL和MongoDB两个数据库?

时间:2018-02-19 07:42:28

标签: php mysql database mongodb laravel-5.5

我想在我的Laravel应用程序中使用MySQL和MongoDB。在这里,我想插入两个数据库(MySQL和MongoDB)并从MongoDB中选择。

1 个答案:

答案 0 :(得分:-1)

连接定义:

  

应用程序/配置/ database.php中

return array(

'default' => 'mysql',

'connections' => array(

    # Primary/Default database connection
    'mysql' => array(
        'driver'    => 'mysql',
        'host'      => 'host1',
        'database'  => 'database1',
        'username'  => 'user1',
        'password'  => 'pass1'
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
    ),

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

);

  

转到架构构建器时:

Schema::connction('mysql2')->create('table', function($table)){
   $table->increments('id')
});
  

查询

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

类SomeModel扩展了Eloquent {

    protected $connection = 'mysql2';

}