Multiple DB Connections in Laravel 5

Struggling to connect different database in Laravel or want to switch different DB connection dynamically? Then this article will solve your query.

Few day back while developing an application I come up to a point where I need to connect to the different database dynamically. I google around and I found few good solution in multiple websites; So I thought to stitch them into a single piece.

Before proceeding we first have to edit our database.php which is located in /config/database.php for Lavael 4.2 it is in app/config/database.php.

Now that we have set the DB connections it’s time to use it

Defining in Model

If have Model which uses a different database then we can define it at the model level like this:

In Eloquent Query

In DB Query

Let me know through the comment if I missed out any point. Cheers!

Default image
Rana Ghosh
I'm a PHP, MySQL, jQuery expert, and designing specialist. My elating approach of seeing web world with a different sight leaves others amazed. My skills include codeigniter, Laravel. I enjoy taking challenges. I love to play FIFA Online 3.
Articles: 1

2 Comments

  1. I think this is a real great article. Much thanks again. Fantastic.

Leave a Reply