Reputation: 33
I have read and tried to do the steps from these links, but my second database still will not configure. Lumen - Create database connection at runtime Lumen Database [xxx] not configured
What am I doing wrong? Is there something I've missed out?
This is my code:
.env file
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=''
in my app.php I have added: $app->configure('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),
],
This is a sample connection to the database that I used:
$checkduplicate = DB::connection('database2')->table('authusers')->select('username')->where('username', $username)->get();
This is in my routes.php
$app->get('database2/register', 'Database2_DashboardController@registerAdmin');
$app->post('database2/register', 'Database2_DashboardController@registerAdmin');
Upvotes: 2
Views: 4761
Reputation: 176
use connection variable 'mysql2' instead of database name 'database2'
$checkduplicate = DB::connection('mysql2')->table('authusers')->select('username')->where('username', $username)->get();
Upvotes: 2