Go to env file and then add the second database configurations →
Now go to config file and copy paste the mysql config with changed name maybe “mysql2”
Inside the configuration change all the env variables accrodingly
👇🏻
- Make a controller to check the functionality
- import DB class inside controller
- write the get command by definining the connection name ( ‘mysql’ or ‘mysql2’)
- By this it will get to know the other database . If you use the DB without connection it is by default ‘mysql’.
With Models
If you want to use Models to intract with database , Same problem you will face as it is by default taking first database
- Go to Model
- add a line of code
public $connection="mysql2";
Now if you import the model and get the data , it will work seamlessly