dtjkl42086 2017-03-28 13:39
浏览 33
已采纳

无法在laravel中设置多个连接

I want to set multiple connections in Laravel application.

I tried using database.php is:

'default' => env('DB_CONNECTION', 'mysql'),
'connections' => [

    'mysql' => [
        'driver' => 'mysql',
        'host' => env('DB_HOST', 'localhost'),
        'port' => env('DB_PORT', '3306'),
        'database' => env('DB_DATABASE', 'reports_db'),
        'username' => env('DB_USERNAME', 'root'),
        'password' => env('DB_PASSWORD', 'pass'),
        'charset' => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix' => '',
        'strict' => false,
        'engine' => null,
    ],
     'mysql1' => [
        'driver' => 'mysql',
        'host' => env('DB_HOST', 'localhost'),
        'port' => env('DB_PORT', '3306'),
        'database' => env('DB_DATABASE', 'a2z'),
        'username' => env('DB_USERNAME', 'root'),
        'password' => env('DB_PASSWORD', 'root'),
        'charset' => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix' => '',
        'strict' => false,
        'engine' => null,
    ],
],

and .env

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=reports_db
DB_USERNAME=root
DB_PASSWORD=pass


CL_DB_CONNECTION=mysql1
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=a2z
DB_USERNAME=root
DB_PASSWORD=root

Inside controller

 public function index()
    {
        $ad= new Ad;

        $ad->setConnection('mysql1');
        $ad =  ad::get();
        echo $job;
      }

But I am not able to set up mysql1 connection.Can you please tell me how to achieve this?

  • 写回答

1条回答 默认 最新

  • doufen3134 2017-03-28 13:43
    关注

    you are actually using the same connection parameters both the time as you are using the same env values in your database.php:

    Your .env file should contain something like this:

    DB_CONNECTION=mysql
    DB_HOST=127.0.0.1
    DB_PORT=3306
    DB_DATABASE=reports_db
    DB_USERNAME=root
    DB_PASSWORD=pass
    
    
    TWO_DB_CONNECTION=mysql1
    TWO_DB_HOST=127.0.0.1
    TWO_DB_PORT=3306
    TWO_DB_DATABASE=a2z
    TWO_DB_USERNAME=root
    TWO_DB_PASSWORD=root
    

    and database.php should be something like this:

    'default' => env('DB_CONNECTION', 'mysql'),
    'connections' => [
    
        'mysql' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', 'localhost'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'reports_db'),
            'username' => env('DB_USERNAME', 'root'),
            'password' => env('DB_PASSWORD', 'pass'),
            'charset' => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix' => '',
            'strict' => false,
            'engine' => null,
        ],
         'mysql1' => [
            'driver' => 'mysql',
            'host' => env('TWO_DB_HOST', 'localhost'),
            'port' => env('TWO_DB_PORT', '3306'),
            'database' => env('TWO_DB_DATABASE', 'a2z'),
            'username' => env('TWO_DB_USERNAME', 'root'),
            'password' => env('TWO_DB_PASSWORD', 'root'),
            'charset' => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix' => '',
            'strict' => false,
            'engine' => null,
        ],
    ],
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言做一个简单的计算器,大家来看看
  • ¥15 nuxtjs3+ts 报错,急呀!
  • ¥15 matlab矩阵复数本征值排序
  • ¥15 skynet MySQL ProtocolBuffers
  • ¥15 浏览器关闭事件有时没执行怎么回事
  • ¥15 使用docker安装chemex后无法启动
  • ¥15 关于#vue.js#的问题:word excel和ppt预览问题语言-javascript)
  • ¥15 Apache显示系统错误3该如何解决?
  • ¥30 uniapp小程序苹果手机加载gif图片不显示动效?
  • ¥20 js怎么实现跨域问题