Multiple database connections in the same Laravel project

Sometimes there is a situation where we have our main project database, but we need to take some external data from another database – for example, where blog is stored, managed by 3rd party software. How to handle it in Laravel 5?

Where to put database credentials

There is a file config/database.php which contains an array of all possible connections- MySQL, PostgreSQL, SQL Server. So the “trick” here is we can add more than one connection with the same database system, let’s say it will be MySQL. So, let’s copy a default array item and paste as another one underneath – and call it mysql_external:

Now, let’s change the ENV variables to different ones, which we will add into our .env file. For example, from DB_HOST – to DB_EXT_HOST and same with others.

And now we have two options: provide those external credentials here in the same config/database.php file, or add them into .env file, which is in your main Laravel folder, like this:

How to connect to the second database

So, we have a connection named mysql_external which has the credentials provided, now we can actually use it. So in your code, where you need to take the data from that external database, you just call DB::connection() method and assign the result to the variable. Then we can use that variable to make any queries.

And that’s it, basically. You can then use any DB functions to get or insert the data into your external database.

Check out my new online course: Laravel Eloquent: Expert Level

12 thoughts on “Multiple database connections in the same Laravel project

  1. You can use Eloquent as well with multiple databases. In your model file like you define guard/filled etc, add in a protected connection variable with the name of the new connection and your Eloquent model will now pull from the new database without having to reset your connection each time. I always define connection in my Eloquent models even if I’m pulling from the default database – mostly because it insures that I won’t be pulling from a persistant database connection that was last opened.

      1. I think what Alicia meant is you can just add the following line to your Eloquent model which will use the other database connection :
        protected $connection = ‘mysql_external’;
        rather than define an explicit call every time like this:

  2. Hello, i have a quick question, how can we connect on random database? In above example we know the name of the external DB is ‘forge’. But we have a situation where on each signup a different company database is also created, some of the data will be inserted on Company DB and some in a common database. So, on loggin in , data are fetched from client specific DB as well. Can we manipulate ‘database’ => env(‘DB_EXT_DATABASE’, ‘forge’) , or is there a better way. Pls suggest, thanks.

  3. This is useful, thanks.

    I currently have a requirement to actually switch between RDBMS’s. If I configure both a mysql and an sqlserver database in my db config file do you know of a way to dynamically switch drivers between queries so I can pull data from both systems relatively seamlessly? I’m using Laravel 5.1.


    Rob Ganly

  4. I am brand new to Laravel. Uneducated at the highest level. The information to include in the .env file is the missing link in so many searches. Thank you for explaining that.

  5. Hi der, How do i connect my local server(which contains codes) with remote database in laravel. if you have any example or video it will be very helpfull.

  6. Thanks very much.
    now I have a different situation.
    I have Mysql database on my laravel site and anything is working normally.
    but I want sometime user from own web browser connects to Microsoft SQL database that stored on his computer.
    any idea for this one?
    all user sql database have same name and same ip address and same password.
    i just want to find a way to connect their dataabse from internet browser and store data on it.

Leave a Reply

Your email address will not be published. Required fields are marked *