2017-02-22 72 views
2

你好我在配置/ database.php中的前綴(MySQL的)是這樣的:覆蓋表前綴

 'prefix' => 'myprefix_', 

但我需要的,只是一個模型,使用不同的前綴,如:

 protected $table = 'otherprefix_mytable'; 

以這種方式laravel尋找「myprefix_otherprefix_mytable」。

任何幫助?

回答

3

在你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

0

默認表前綴可在laravel模型中覆蓋以下示例:


public function __construct(array $attributes = array()) { 
    $colletion = Config::get('database'); 
    $collection['connections']['mysql']['prefix'] = 'consultancy_'; 
    Config::set('database',$collection); 
    parent::__construct($attributes); 
}