Hi devs, in this quick tip, i’ll show you how to add multiple database connections to your laravel project. let’s see how we can do this.

First, add second database connection variable in .env file.

Next, define database connection in config/database.php file.

 

Read Also  How to check request is ajax or not in Laravel

Now, we are done with configuration. We have our default connection still set to mysql. This means that, unless we specify otherwise, the application will use thet mysql connection. Let’s see how we can use it.

 

Schema

To specify which connection to use, simply run the connection() method.

 

Query Builder

 

Eloquent

Set the $connection variable in your model

You can also define the connection at runtime via the setConnection method or the on static method:

 

I hope it can help you.


No Comments

Leave a Reply

your email address will not be published. required fields are marked *

*
*