执行必要步骤后未配置 Lumen 数据库 []

Lumen database [] not configured after doing necessary steps

我已阅读并尝试执行这些链接中的步骤,但我的第二个数据库仍然无法配置。

我做错了什么?我错过了什么吗?

这是我的代码:

.env 文件

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=database1
DB_USERNAME=root
DB_PASSWORD=''

DB_CONNECTION=mysql2
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=database2
DB_USERNAME=root
DB_PASSWORD=''

在我的 app.php 中我添加了: $app->配置('database');

database.php

'connections' => [

    'mysql' => [
        'driver'    => 'mysql',
        'host'      => env('DB_HOST', 'localhost'),
        'port'      => env('DB_PORT', 3306),
        'database'  => env('DB_DATABASE', 'database1'),
        'username'  => env('DB_USERNAME', 'root'),
        'password'  => env('DB_PASSWORD', ''),
        'charset'   => env('DB_CHARSET', 'utf8'),
        'collation' => env('DB_COLLATION', 'utf8_unicode_ci'),
        'prefix'    => env('DB_PREFIX', ''),
        'timezone'  => env('DB_TIMEZONE', '+00:00'),
        'strict'    => env('DB_STRICT_MODE', false),
    ],

    'mysql2' => [
        'driver'    => 'mysql',
        'host'      => env('DB_HOST', 'localhost'),
        'port'      => env('DB_PORT', 3306),
        'database'  => env('DB_DATABASE', 'database2'),
        'username'  => env('DB_USERNAME', 'root'),
        'password'  => env('DB_PASSWORD', ''),
        'charset'   => env('DB_CHARSET', 'utf8'),
        'collation' => env('DB_COLLATION', 'utf8_unicode_ci'),
        'prefix'    => env('DB_PREFIX', ''),
        'timezone'  => env('DB_TIMEZONE', '+00:00'),
        'strict'    => env('DB_STRICT_MODE', false),
    ],

这是我使用的数据库连接示例:

$checkduplicate = DB::connection('database2')->table('authusers')->select('username')->where('username', $username)->get();

这是我的 routes.php

$app->get('database2/register', 'Database2_DashboardController@registerAdmin');

$app->post('database2/register', 'Database2_DashboardController@registerAdmin');

使用连接变量'mysql2'代替数据库名称'database2'

 $checkduplicate = DB::connection('mysql2')->table('authusers')->select('username')->where('username', $username)->get();