如何在Laravel中使用多个数据库 [英] How to use multiple database in Laravel

查看:224
本文介绍了如何在Laravel中使用多个数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的系统中合并多个数据库。大多数时候数据库是MySQL;但它在将来可能不同,即管理员可以生成异构数据库系统的使用来源。



所以我的问题是 Laravel提供任何门面来处理这种情况吗?或任何其他框架有更适合的问题的能力是?

解决方案

定义连接

您的数据库配置文件内部 - 可能 app / config / database.php

  return array(

'default'=>'mysql',

'connections'=> array (

#我们的主数据库连接
'mysql'=> array(
'driver'=>'mysql',
'host'=> 'host1',
'database'=>'database1',
'username'=>'user1',
'password'=>'pass1'
' charset'=>'utf8',
'collat​​ion'=>'utf8_unicode_ci',
'prefix'=>'',
),

#我们的辅助数据库连接
'mysql2'=> array(
'driver'=>'mysql',
'host'=> 'host2',
'database'=> 'database2',
'username'=> 'user2',
'password'=> 'pass2'
'charset'=> 'utf8',
'collat​​ion'=> 'utf8_unicode_ci',
'prefix'=> '',
),
),
);

架构



在Schema Builder中,您可以将Schema facade与任何连接一起使用。要指定要使用的连接,只需运行 connection()方法:

  Schema :: connection('mysql2') - > create('some_table',function($ table)
{
$ table-> increment('id'):
});

查询



类似于Schema Builder,您可以在查询生成器上定义连接:

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

雄辩



一个方法是设置 $ connection 您的模型中的变量:

 <?php 

class SomeModel extends Eloquent {

protected $ connection ='mysql2';

}

您也可以在运行时通过 setConnection 方法

 <?php 

SomeController扩展BaseController {

public function someMethod()
{
$ someModel = new SomeModel;

$ someModel-> setConnection('mysql2');

$ something = $ someModel-> find(1);

return $ something;
}

}




注意请注意尝试与数据库中的表建立关系!这是可能的,但它可以带有一些注意事项,取决于你有什么数据库和/或数据库设置。


strong> 来源


I want to combine multiple databases in my system. Most of the time the database is MySQL; but it may differ in future i.e. Admin can generate such a reports which is use source of heterogeneous database system.

So my question is does Laravel provide any Facade to deal with such situations? Or any other framework have more suitable capabilities for problem is?

解决方案

Define Connections

Inside of your database configuration file - likely app/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'    => '',
        ),
    ),
);

Schema

Within the Schema Builder, you can use the Schema facade with any connection. To specify which connection to use, simply run the connection() method:

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

Query

Similar to Schema Builder, you can define a connection on the Query Builder:

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

Eloquent

You can also define which connection to use in your Eloquent models as well!

One way is to set the $connection variable in your model:

<?php

class SomeModel extends Eloquent {

    protected $connection = 'mysql2';

}

You can also define the connection at runtime via the setConnection method

<?php

class SomeController extends BaseController {

    public function someMethod()
    {
        $someModel = new SomeModel;

        $someModel->setConnection('mysql2');

        $something = $someModel->find(1);

        return $something;
    }

}

Note Be careful about attempting to build relationships with tables across databases! It is possible to do, but it can come with some caveats and depends on what database and/or database settings you have.

Source

这篇关于如何在Laravel中使用多个数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆