2017-02-22 1 views
2

Hallo Ich habe in config/database.php einen Präfix (mysql) wie folgt aus:Aufschalten Tabellen-Präfix für ein Modell

 'prefix' => 'myprefix_', 

Aber ich brauche, nur für ein Modell, einen anderen Präfix zu verwenden, wie:

 protected $table = 'otherprefix_mytable'; 

Auf diese Weise laravel auf der Suche nach "myprefix_otherprefix_mytable".

Irgendwelche Hilfe?

Antwort

3

In Ihren app/config/database.php make 2 verschiedenen Verbindungen wie

'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', 
     ), 
    ), 

Und dann später in Modell können Sie verschiedene Verbindung verwenden

class SomeModel extends Eloquent {  
    protected $connection = 'mysql2';  
} 

Weitere Hilfe Scheck this

0

Präfix Standardtabelle sein kann Überschreibe im Laravel-Modell wie folgt:


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