您好我在config / database.php中有一个前缀(mysql),如下所示:
'prefix' => 'myprefix_',
但是,我只需要一个模型,使用不同的前缀,如:
protected $table = 'otherprefix_mytable';
以这种方式laravel寻找“myprefix_otherprefix_mytable”。
任何帮助?
答案 0 :(得分:8)
在app/config/database.php
制作2个不同的连接,例如
'connections' => array(
# first prefix
'mysql1' => array(
'driver' => 'mysql',
'host' => 'host1',
'database' => 'database1',
'username' => 'user1',
'password' => 'pass1'
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => 'prefix1',
),
# second prefix
'mysql2' => array(
'driver' => 'mysql',
'host' => 'host1',
'database' => 'database1',
'username' => 'user1',
'password' => 'pass1'
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => 'prefix2',
),
),
然后在模型中你可以使用不同的连接
class SomeModel extends Eloquent {
protected $connection = 'mysql2';
}
如需更多帮助,请查看this
答案 1 :(得分:1)
默认表格前缀可以覆盖在laravel模型中,如下例所示:
public function __construct(array $attributes = array()) {
$colletion = Config::get('database');
$collection['connections']['mysql']['prefix'] = 'consultancy_';
Config::set('database',$collection);
parent::__construct($attributes);
}