Laravel | How to use multiple databases in Laravel?

In this post, we will discuss How to use multiple databases in Laravel which we can perform using separate database connections for each database in the config/ database.php file for the same we have given an example of code to understand it better.

Laravel

Laravel

We have given an example here to understand it in better ways and implement it.

'connections' => [
    
    'mysql' => [
        'driver' => 'mysql',
        'host' => env(' DB_HOST', '127.0.0.1'),
        'port' => env(' DB_PORT', '3306'),
        'database' => env(' DB_DATABASE', 'database1'),
        'username' => env(' DB_USERNAME', 'root'),
        'password' => env(' DB_PASSWORD', ''),
        'unix_socket' => env(' DB_SOCKET', ''),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'strict' => true,
        'engine' => null,
    ],

    'database2' => [
        'driver' => 'mysql',
        'host' => env(' DB_HOST_2', '127.0.0.1'),
        'port' => env(' DB_PORT_2', '3306'),
        'database' => env(' DB_DATABASE_2', 'database2'),
        'username' => env(' DB_USERNAME_2', 'root'),
        'password' => env(' DB_PASSWORD_2', ''),
        'unix_socket' => env(' DB_SOCKET_2', ''),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'strict' => true,
        'engine' => null,
    ],
    
],

Here in the example above we have taken two database as MySQL and database2 for connections. Where we have specified each connection has its own set of database connection parameters, including the database name, host, port, username, and password.

For using the connections to our project or application we may specify using the connection in our model or database query. And we have given a line of code to get the data from the MySQL database and from the users table which already exist in it.

$users = DB:: connection( 'mysql')-> table(' users')-> get();

To get the data from another database that was database2 which has a table as posted the code is given below which will help you in getting the data from the second database.

$posts = DB::connection( 'database2')->table(' posts')-> get();

Here we have tried to get the data using two different databases which simply shows we had established the connection between both databases which we have the objective here.

Now as we know that we can also be assigned one of the databases as the default database to get the data we can perform a command given below which would help us in getting things done at a faster pace by setting the db connection variable in our .env file. For the same follow the code given below.

DB_ CONNECTION= mysql

Where it will make MySQL connection the default connection for our project or application. And at some time we want to switch to another database we can do so by specifying it explicitly in our Eloquent models or database queries.

 

 

 

To learn more about How to use multiple databases in Laravel visit: by stack overflow.

To learn more about MySQL tutorials and the solutions to the problems along with concepts and tutorials of solutions list and learn visit: MySQL Tutorials And Problems.

Leave a Comment

%d bloggers like this: