Laravel 5.5 multiple database connection example


Hi Guys. In this article i will let you know how to use multiple database in laravel 5.5 application. Laravel 5.5 provide several database connection like mysql, sqlite, pgsql, sqlsrv, mongodb etc. So it could always be simple to use following databases connection.

But if you require two database connection in same laravel application then what you will do and how you can. So laravel 5.5 provide very simple way to do this. We can use different database connection multiple. You can also use two or more database connections. So let's see how you can do this thing.

Here, laravel 5.5 make configuration from .env file, So first you have to open .env file and add your both database connection details like as bellow:

.env

DB_CONNECTION=mysql

DB_HOST=127.0.0.1

DB_PORT=3306

DB_DATABASE=laravel_test

DB_USERNAME=root

DB_PASSWORD=root


DB_CONNECTION_2=mysql

DB_HOST_2=127.0.0.1

DB_PORT_2=3306

DB_DATABASE_2=m_newcrm

DB_USERNAME_2=root

DB_PASSWORD_2=root

Now we are ready to add another configuration file database.php and there we have to add connections array details, So let's simple add this way as like now:

config/database.php

<?php


return [


'default' => env('DB_CONNECTION', 'mysql'),


'connections' => [


'sqlite' => [

'driver' => 'sqlite',

'database' => env('DB_DATABASE', database_path('database.sqlite')),

'prefix' => '',

],


'mysql' => [

'driver' => 'mysql',

'host' => env('DB_HOST', '127.0.0.1'),

'port' => env('DB_PORT', '3306'),

'database' => env('DB_DATABASE', 'forge'),

'username' => env('DB_USERNAME', 'forge'),

'password' => env('DB_PASSWORD', ''),

'unix_socket' => env('DB_SOCKET', ''),

'charset' => 'utf8mb4',

'collation' => 'utf8mb4_unicode_ci',

'prefix' => '',

'strict' => true,

'engine' => null,

],


'mysql2' => [

'driver' => 'mysql',

'host' => env('DB_HOST_2', '127.0.0.1'),

'port' => env('DB_PORT_2', '3306'),

'database' => env('DB_DATABASE_2', 'forge'),

'username' => env('DB_USERNAME_2', 'forge'),

'password' => env('DB_PASSWORD_2', ''),

'unix_socket' => env('DB_SOCKET_2', ''),

'charset' => 'utf8mb4',

'collation' => 'utf8mb4_unicode_ci',

'prefix' => '',

'strict' => true,

'engine' => null,

],


'pgsql' => [

'driver' => 'pgsql',

'host' => env('DB_HOST', '127.0.0.1'),

'port' => env('DB_PORT', '5432'),

'database' => env('DB_DATABASE', 'forge'),

'username' => env('DB_USERNAME', 'forge'),

'password' => env('DB_PASSWORD', ''),

'charset' => 'utf8',

'prefix' => '',

'schema' => 'public',

'sslmode' => 'prefer',

],


'sqlsrv' => [

'driver' => 'sqlsrv',

'host' => env('DB_HOST', 'localhost'),

'port' => env('DB_PORT', '1433'),

'database' => env('DB_DATABASE', 'forge'),

'username' => env('DB_USERNAME', 'forge'),

'password' => env('DB_PASSWORD', ''),

'charset' => 'utf8',

'prefix' => '',

],


],


'migrations' => 'migrations',


'redis' => [


'client' => 'predis',


'default' => [

'host' => env('REDIS_HOST', '127.0.0.1'),

'password' => env('REDIS_PASSWORD', null),

'port' => env('REDIS_PORT', 6379),

'database' => 0,

],


],


];

Ok, now we are ready to use both connection, "mysql" is default connection, if you want to use "mysql2" then you have to specify connection before use query builder. So let's see both routes how we can run query.

routes/web.php

Route::get('check-connection', function () {

$users = DB::table("users")->get();

dd($users);

});


Route::get('check-connection-2', function () {

$users = DB::connection("mysql2")->table("users")->get();

dd($users);

});

As you can see above both routes result, you can understand how it is works. So let's use it :)

I hope you found best solution....


September 14, 2017 | 7714 Viewer | Category : PHP Laravel
Related Posts: