Thứ Ba, 11 tháng 10, 2016

Laravel 5 Tutorial : Create Multi Databases Connection in laravel 5.3


Laravel 5 Tutorial : Working with database in Laravel 5.3, How to create multi connection into MySQL databases in laravel 5.3. Laravel 5.3 Php framework can handle an application with multi databases access.

At the previews lessons, we have learn how to working with database in laravel, please read How to create connection into database

Multi Connection

In laravel 5.3 database configuration for our laravel project is located at config/database.php. This file contain database connections. You may define all of your database connections in this file. You may specify which connection should be used by default in return array like.

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

This file included examples for all of the supported database systems. Now let's start how to connect multiple databases in laravel 5.3.

Create Multi Databases Connection in laravel 5.3

First step - Open your laravel project and at the config/database.php add  and declare new multiple databases under connections array, see this default from laravel installations.

'mysql' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', 'localhost'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'charset' => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix' => '',
            'strict' => true,
            'engine' => null,
        ],

now, to make multi connection edit database.php above and add new connection function. For examples :

'mysql' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', 'localhost'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'yourdb1'),
            'username' => env('DB_USERNAME', 'yourID'),
            'password' => env('DB_PASSWORD', 'yourpass'),
            'charset' => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix' => '',
            'strict' => true,
            'engine' => null,
        ],
        'mysql2' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', 'localhost'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'yourdb2'),
            'username' => env('DB_USERNAME', 'yourID'),
            'password' => env('DB_PASSWORD', 'yourpass'),
            'charset' => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix' => '',
            'strict' => true,
            'engine' => null,
        ],

Now we have added to databases in this file with named "yourdb1" and "yourdb2". So initially we have defined our databases. Now we need to play with our multiple databases.

Configure .ENV file

Go to "root/.env" file and remove initial database configuration (remove code looks like below) For more about Environment Configuration. Save .env file and run you queries.

DB_HOST=localhost
DB_DATABASE=''
DB_USERNAME=''
DB_PASSWORD=''

Note : remove all line connection configurations.

Accessing connection and running query with Query Builder :

Next, we can access each connection via the connection() method on the DB facade. The name passed to the connection method should correspond to one of the connections listed in your config/database.php configuration file. To run query you need to make first DB object then you can access data with this object easily like this :

// this will Running query with default connection.
$userArray = DB::table('biodata')->get();
print_r($userArray);

// This will Makeing an object of second DB.             
$users2 = DB::connection('mysql2');
// Getting data with second DB object.
$u = $users2->table('book')->get();
print_r($u);

There are more methods to Connect multiple databases in laravel and access connections.

The default and easy i have shared to connect multiple databases in laravel and access connections and run your queries.


List Video Tutorial laravel 5.3



See you next lessons ...

Không có nhận xét nào:

Đăng nhận xét