|
Answer» To use multiple databases in Laravel, FOLLOW these steps carefully. - Ensure these settings in the .env file
- Add these following LINES of CODE in the config/database.php file to clearly define the relationship between the two databases
- EXECUTE the query with particular database.
1. Ensure these settings in the .env fileDB_CONNECTION=mysql DB_HOST=localhost DB_PORT=3306 DB_DATABASE=your_db_name DB_USERNAME=bestinterviewquestion [email protected] DB_CONNECTION_SECOND=mysql DB_HOST_SECOND=localhost DB_PORT_SECOND=3306 DB_DATABASE_SECOND=your_db_name2 DB_USERNAME_SECOND=bestinterviewquestion [email protected] 2. Add these following lines of code in the config/database.php file to clearly define the relationship between the two databases'mysql' => [ 'driver' => env('DB_CONNECTION'), 'host' => env('DB_HOST'), 'port' => env('DB_PORT'), 'database' => env('DB_DATABASE'), 'username' => env('DB_USERNAME'), 'password' => env('DB_PASSWORD'), ], 'mysql2' => [ 'driver' => env('DB_CONNECTION_SECOND'), 'host' => env('DB_HOST_SECOND'), 'port' => env('DB_PORT_SECOND'), 'database' => env('DB_DATABASE_SECOND'), 'username' => env('DB_USERNAME_SECOND'), 'password' => env('DB_PASSWORD_SECOND'), ], 3. Execute Query$users = DB::connection('your_db_name2')->select();
|