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.

nikhilpatel

nikhilpatel

Hello Folks, I am PHP, laravel Developer having 5+ years of experience. I live in India and I like to write tips & tutorials so it can be helpful for IT folks. it will help to get a better solution for their projects. I am also a fan of Jquery, Vue JS, ReactJS, and bootstrap from the primitive stage.

Leave a Reply