🪀

Multiple Database Connections

Go to env file and then add the second database configurations →
notion image
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
notion image

👇🏻

notion image
  1. Make a controller to check the functionality
  1. import DB class inside controller
  1. write the get command by definining the connection name ( ‘mysql’ or ‘mysql2’)
  1. By this it will get to know the other database . If you use the DB without connection it is by default ‘mysql’.
notion image
 

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
  1. Go to Model
  1. add a line of code
public $connection="mysql2";
notion image
Now if you import the model and get the data , it will work seamlessly
notion image