Laravel – Multiple Database Connections Example

Laravel – Multiple Database Connections Example

In this post we will give you information about Laravel – Multiple Database Connections Example. Hear we will give you detail about Laravel – Multiple Database Connections ExampleAnd how to use it also give you demo for it if it is necessary.

you can set multiple database connections in same laravel application. Laravel provide configration file for setup multiple database connections. you can see config/database.php file and see mysql multiple database connections. you can add setting another mysql database detail like this way:

config/database.php

return [


'fetch' => PDO::FETCH_CLASS,


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


'connections' => [

'sqlite' => [

'driver' => 'sqlite',

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

'prefix' => '',

],


'mysql' => [

'driver' => 'mysql',

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

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

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

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

'charset' => 'utf8',

'collation' => 'utf8_unicode_ci',

'prefix' => '',

'strict' => false,

'engine' => null,

],


'mysql2' => [

'driver' => 'mysql',

'host' => 'localhost',

'database' => 'college',

'username' => 'root',

'password' => 'root',

'charset' => 'utf8',

'collation' => 'utf8_unicode_ci',

'prefix' => '',

'strict' => false,

'engine' => null,

],


'pgsql' => [

'driver' => 'pgsql',

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

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

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

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

'charset' => 'utf8',

'prefix' => '',

'schema' => 'public',

],


'sqlsrv' => [

'driver' => 'sqlsrv',

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

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

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

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

'charset' => 'utf8',

'prefix' => '',

],


],


'migrations' => 'migrations',


'redis' => [


'cluster' => false,


'default' => [

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

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

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

'database' => 0,

],


],


];

You can use multiple mysql database like this way:

Example:

Also see:How to Insert Multiple Records in Laravel?

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

print_r($users);

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

print_r($users);

Hope this code and post will helped you for implement Laravel – Multiple Database Connections Example. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

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

74  +    =  80

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US