How to use multiple databases in Laravel?

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 file DB_CONNECTION=mysql DB_HOST=localhost DB_PORT=3306 DB_DATABASE=your_db_name DB_USERNAME=bestinterviewquestion DB_PASSWORD=admin@123 DB_CONNECTION_SECOND=mysql DB_HOST_SECOND=localhost DB_PORT_SECOND=3306 DB_DATABASE_SECOND=your_db_name2 DB_USERNAME_SECOND=bestinterviewquestion DB_PASSWORD_SECOND=admin@12345 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();

Popular posts from this blog

Ajax Toggle(on/off) on-click Update

Yii Framework In Update Time View Image In Form