laravel 如何在 Lumen 中使用多个数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45729492/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:30:46  来源:igfitidea点击:

How to use multiple database in Lumen

laravellumenlumen-5.2lumen-5.3

提问by Govind Samrow

We've using Lumen for building API's , Now we need to access multiple databases.

我们已经使用 Lumen 来构建 API,现在我们需要访问多个数据库。

Currently using .envfor database config but unable to found the way to multiple databases in .env

当前.env用于数据库配置但无法找到多个数据库的方法.env

where we need to read 2nd connection ...

我们需要阅读第二个连接的地方......

回答by Hiren Gohel

First, you'll need to configure your connections. If you don't already have one you'll need to create a configdirectory in your project and add the file config/database.php. It might look like this:

首先,您需要配置连接。如果你还没有,你需要config在你的项目中创建一个目录并添加文件config/database.php. 它可能看起来像这样:

<?php

return [

   'default' => 'accounts',

   'connections' => [
        'mysql' => [
            'driver'    => 'mysql',
            'host'      => env('DB_HOST'),
            'port'      => env('DB_PORT'),
            'database'  => env('DB_DATABASE'),
            'username'  => env('DB_USERNAME'),
            'password'  => env('DB_PASSWORD'),
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
            'strict'    => false,
         ],

        'mysql2' => [
            'driver'    => 'mysql',
            'host'      => env('DB2_HOST'),
            'port'      => env('DB_PORT'),
            'database'  => env('DB2_DATABASE'),
            'username'  => env('DB2_USERNAME'),
            'password'  => env('DB2_PASSWORD'),
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
            'strict'    => false,
        ],
    ],
];

Once you've added your connectionconfigurations, you can access them by getting the database manager object out of the container and calling ->connection('connection_name').

添加connection配置后,您可以通过从容器中取出数据库管理器对象并调用->connection('connection_name').

// Use default connection
app('db')->connection()->select('xx');
DB::connection()->select('yy');

// Use mysql2 connection
app('db')->connection('mysql2')->select('xx');
DB::connection('mysql2')->select('yy');

Hope this helps you!!

希望这对你有帮助!!

回答by Paul

This also worked. In the current version of Lumen 5.7

这也奏效了。在当前版本的 Lumen 5.7

config/database.php

配置/数据库.php

<?php

return [

    'default' => env('DB_CONNECTION', 'sqlsrv'),
    'migrations' => 'migrations',
    'connections' => [
        'sqlsrv' => [
            'driver' => 'sqlsrv',
            'host' => env('DB_HOST', 'localhost'),
            'port' => env('DB_PORT', '1433'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'charset' => 'utf8',
            'prefix' => '',
            'prefix_indexes' => true,
        ],

        'sqlsrv2' => [
            'driver' => 'sqlsrv',
            'host' => env('DB_HOST', 'localhost'),
            'port' => env('DB_PORT', '1433'),
            'database' => env('DB_DATABASE2', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'charset' => 'utf8',
            'prefix' => '',
            'prefix_indexes' => true,
        ],
    ],
];

.env

.env

DB_CONNECTION=sqlsrv
DB_HOST=localhost
DB_PORT=1433
DB_DATABASE=database1
DB_USERNAME=username
DB_PASSWORD=password

DB_DATABASE2=database2

Usage:

用法:

Model: protected $connection = 'sqlsrv2';Other: ->connection('sqlsrv2')

型号:protected $connection = 'sqlsrv2';其他:->connection('sqlsrv2')

I hope i help you!

我希望我能帮助你!

Reference:https://fideloper.com/laravel-multiple-database-connections

参考:https: //fideloper.com/laravel-multiple-database-connections