Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 更改laravel模型中的数据库连接_Php_Laravel_Laravel 4 - Fatal编程技术网

Php 更改laravel模型中的数据库连接

Php 更改laravel模型中的数据库连接,php,laravel,laravel-4,Php,Laravel,Laravel 4,因此,我使用的是Laravel 4.2,我想要的是在我的一个模型中使用外部数据库,这是我的模型代码: <?php class McibModel extends Eloquent { /** * The database table used by the model. * * @var string */ //here i should call the external database table protect

因此,我使用的是Laravel 4.2,我想要的是在我的一个模型中使用外部数据库,这是我的模型代码:

<?php
class McibModel extends Eloquent {
    /**
      * The database table used by the model.
      *
      * @var string
      */
    //here i should call the external database table
    protected $table = 'agencies';
}

不同的型号可以有不同的数据库连接。因此,您的模型使用正常的默认连接,但您的“McibModel”模型可以使用其他连接:

class McibModel extends Model {

    protected $connection= 'second_db_connection';

    protected $table = 'agencies';

}
然后在您的DB连接文件中-您将有如下内容:

return array(
    'connections' => array(
        'mysql' => array(
            'driver'    => 'mysql',
            'host'      => 'localhost',
            'database'  => 'database1',
            'username'  => 'user1',
            'password'  => 'pass1'
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
        ),

        'second_db_connection' => array(
            'driver'    => 'mysql',
            'host'      => 'localhost',
            'database'  => 'database2',
            'username'  => 'user2',
            'password'  => 'pass2'
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
        ),
    ),
$McibModel = new McibModel();
$McibModel->setConnection('second_db_connection');

我认为对于许多用例来说,在运行时这样声明连接是很方便的:

return array(
    'connections' => array(
        'mysql' => array(
            'driver'    => 'mysql',
            'host'      => 'localhost',
            'database'  => 'database1',
            'username'  => 'user1',
            'password'  => 'pass1'
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
        ),

        'second_db_connection' => array(
            'driver'    => 'mysql',
            'host'      => 'localhost',
            'database'  => 'database2',
            'username'  => 'user2',
            'password'  => 'pass2'
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
        ),
    ),
$McibModel = new McibModel();
$McibModel->setConnection('second_db_connection');

在某种程度上,设置受保护属性将始终将特定模型连接到数据库

class MyModel extends Model {

     protected $connection = "second_db_connection";
}
在查询中,我喜欢这种方法

(new MyModel())->on('second_db_connnection')->get();

您可以这样做来设置另一个连接:

$product= new Product();
$product->setConnection('another_connection');

还要注意
on()
方法,它让您可以动态地指定连接:
McibModel::on('second\u db\u connection')->get()
正是我要找的@lukasgeiter