本文介绍了覆盖模型的表前缀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我在config/database.php中有一个这样的前缀(mysql):

Hello I've in config/database.php a prefix (mysql) like this:

     'prefix' => 'myprefix_',

但是我只需要为一个模型使用一个不同的前缀,如:

But I need, only for one model, to use a different prefix like:

     protected $table = 'otherprefix_mytable';

通过这种方式,laravel查找"myprefix_otherprefix_mytable".

In this way laravel looking for "myprefix_otherprefix_mytable".

有帮助吗?

推荐答案

在您的app/config/database.php中建立2个不同的连接,例如

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

这篇关于覆盖模型的表前缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 20:38