Hello Folks, we are back with something very interesting topic. Today we are going to learn how we can use multiple database connections for an eloquent relationship with Laravel. So let’s start.
If you are new and don’t know how to setup laravel then don’t worry you can start from scratch by clicking this link Laravel Setup & Simple CRUD App.
Sometimes we are required to use two different databases in our laravel application. It may be for different reasons. Are you planning to use multiple databases in a single application and worrying about an eloquent relationship?
Laravel has an amazing feature that supports different database connection same time. It means we can use PostgreSQL, MySql, and SQL Server same time. And we can easily set relationships across the different databases.
Today we will try to implement two different databases in the same laravel application. We are going to use PostgreSQL and MySql databases. We will also see how we can set validation over different database tables.
Note: we are not going to show how to set up laravel here so you can check out our post : Laravel Setup & Simple CRUD App
ENV and database configuration
After installing laravel we need to add PostgreSQL and MySQL configuration in the .env file and also need to make changes in the database.php file.
So our .env file looks like this.
database.php file looks like this.
Migration
Next, we will create migration for PostgreSQL and MySQL. We will create two tables User table and the task table.
User Migration
Task Migration
In the above migration file, we have defined the connection to pgsql. It will automatically connect with pgsql and check if the table does not exist then it will create a new table.
Models configuration
Next, we will be setting up the model for the relationship.
You can also define which connection to use in your Eloquent models as well!
One way is to set the $connection variable in your model.
You can also define the connection at runtime via the setConnection method.
Same as before we were doing to get data using model, we can get data.
Here, I have skipped the seeder part. we can add manually data for testing purposes.
Validations
For validation, we can pass the connection name with the table name that’s it. Laravel will check validation for the given connection.
In the above example, we have passed the connection name. which is the second database settings array in the database.php file.
Occasionally, you may need to set a
For ex: we are filtering tasks by user_id. But we need to validate that the given user id should exist in the database user table. Since our task table is in PostgreSQL as seen above, settings exists:mysql.users as a validation rule will use the MySQL connection. And it will check in MySql whether the given id exists or not.

Utterly composed written content, Really enjoyed examining.
Hi! This is my first visit to your blog! We are a collection of volunteers and starting a new project in a community in the same niche. Your blog provided us useful information to work on. You have done a outstanding job!
Woah! I’m really digging thhe template/theme of this website.
It’s simple, yet effective. A lot of times
it’s difficult tto get thnat “perfect balance”
betfween superb usability and visual appearance. I must sayy that you’ve done a awesome jjob with this.
Additionally, thhe boog loads verry fast for me on Firefox.
Superb Blog! xnxx
It’s actually a nioce annd helpful piuece of information. I am hazppy that you just shared thiks useful info with us.
Pleasse stray uus up tto date like this. Thank you
forr sharing. xnxx
Wonderful web site. Lotts of useful info here. I’m sending it tto a
ffew buddies ans additionallly sharing in delicious. Andd naturally,
thanks for your effort! xnxx