laravel lumen use 2 databases

Solutions on MaxInterview for laravel lumen use 2 databases by the best coders in the world

showing results for - "laravel lumen use 2 databases"
Lia
19 Apr 2017
1First, you'll need to configure your connections. If you don't already have one you'll need to create a config directory in your project and add the file config/database.php. It might look like this:
2
3<?php
4
5return [
6
7   'default' => 'accounts',
8
9   'connections' => [
10        'mysql' => [
11            'driver'    => 'mysql',
12            'host'      => env('DB_HOST'),
13            'port'      => env('DB_PORT'),
14            'database'  => env('DB_DATABASE'),
15            'username'  => env('DB_USERNAME'),
16            'password'  => env('DB_PASSWORD'),
17            'charset'   => 'utf8',
18            'collation' => 'utf8_unicode_ci',
19            'prefix'    => '',
20            'strict'    => false,
21         ],
22
23        'mysql2' => [
24            'driver'    => 'mysql',
25            'host'      => env('DB2_HOST'),
26            'port'      => env('DB_PORT'),
27            'database'  => env('DB2_DATABASE'),
28            'username'  => env('DB2_USERNAME'),
29            'password'  => env('DB2_PASSWORD'),
30            'charset'   => 'utf8',
31            'collation' => 'utf8_unicode_ci',
32            'prefix'    => '',
33            'strict'    => false,
34        ],
35    ],
36];
37Once you've added your connection configurations, you can access them by getting the database manager object out of the container and calling ->connection('connection_name').
38
39// Use default connection
40app('db')->connection()->select('xx');
41DB::connection()->select('yy');
42
43// Use mysql2 connection
44app('db')->connection('mysql2')->select('xx');
45DB::connection('mysql2')->select('yy');
46Hope this helps you!!