change database connection in laravel model(在 laravel 模型中更改数据库连接)
问题描述
所以我使用 Laravel 4.2,我想要在我的一个模型中使用 external 数据库,这是我的模型代码:
So i work with Laravel 4.2, what i want is in one of my models use an external database, this is my model code :
<?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';
}
所以,如果有人有任何想法,我将非常感激.
So please if someone has any idea i will be very appreciative.
推荐答案
不同的模型可以有不同的数据库连接.所以您的模型使用正常的默认连接 - 但您的McibModel"模型可以使用另一个连接:
Different models can have different database connections. So your models use the normal default connection - but your 'McibModel' model can use another connection:
class McibModel extends Model {
protected $connection= 'second_db_connection';
protected $table = 'agencies';
}
然后在您的数据库连接文件中 - 您将拥有如下内容:
Then in your DB connection file - you would have something like this:
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' => '',
),
),
这篇关于在 laravel 模型中更改数据库连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 laravel 模型中更改数据库连接


- 带有通配符的 Laravel 验证器 2021-01-01
- 如何定位 php.ini 文件 (xampp) 2022-01-01
- Laravel 仓库 2022-01-01
- 没有作曲家的 PSR4 自动加载 2022-01-01
- 从 PHP 中的输入表单获取日期 2022-01-01
- SoapClient 设置自定义 HTTP Header 2021-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- Mod使用GET变量将子域重写为PHP 2021-01-01
- PHP Count 布尔数组中真值的数量 2021-01-01
- 正确分离 PHP 中的逻辑/样式 2021-01-01