Lorem Ipsum
Lorem Ipsum

Reputation: 41

How do get data from two databases in Laravel 5.3?

So I would like to get data from two mysql database. By default you define one on .env file and also config/database.php.

I now want to add another external database. How can I achieve this? Can I define two or more databases in .env and config/database.php?

Anyone help. Thank you:-

Upvotes: 2

Views: 451

Answers (1)

Danyal Sandeelo
Danyal Sandeelo

Reputation: 12391

That's how you do it:

 # Knust primary database connection
 'mysql' => [
    'driver'    => 'mysql',
    'host'      => env('DB_HOST', 'localhost'),
    'database'  => 'hofsa_knust',
    'username'  => 'root',
    'password'  => '',
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => '',
    'strict'    => false,
],

       # Legon primary database connection
     'mysql2' => [
    'driver'    => 'mysql',
    'host'      => env('DB_HOST', 'localhost'),
    'database'  => 'hofsa_legon',
    'username'  => 'root',
    'password'  => '',
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => '',
    'strict'    => false,
],

This is a good tutorial on how to use miltiple databases in laravel.

Upvotes: 1

Related Questions