Override table prefix for a model

浪子不回头ぞ 提交于 2019-12-04 10:03:59

In your app/config/database.php make 2 different connections like

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

And then later in model you can use different connection

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

For more help check this

Default table prefix can be overriding in laravel model as following example:


public function __construct(array $attributes = array()) {
    $colletion = Config::get('database');
    $collection['connections']['mysql']['prefix'] = 'consultancy_';
    Config::set('database',$collection);
    parent::__construct($attributes);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!