Laravel 5 Tutorial : Create Multi Databases Connection in laravel 5.3

Laravel 5.3 Tutorial for beginners : How to create database connection in Laravel 5.3? make multi connection to MySQL database in laravel.

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 ...

COMMENTS


Feel free to code it up and send us a pull request.

Hi everyone, let's me know how much this lesson can help your work. Please Subscribe and Follow Our Social Media 'kodeajaib[dot]com' to get Latest tutorials and will be send to your email everyday for free!, Just hit a comment if you have confused. Nice to meet you and Happy coding :) all ^^



Follow by E-Mail


Name

ADO.NET,3,Ajax,6,Android,9,AngularJS,4,ASP.NET,4,Blogger Tutorials,7,Bootstrap,7,C++,1,Codeigniter,2,Cplusplus,6,Crystal Report,6,CSharp,25,Ebook Java,2,FlyExam,1,FSharp,3,Game Development,2,Java,35,JDBC,2,Laravel,89,Lumen,2,MariaDB,2,Ms Access,3,MySQL,31,ODBC,6,OleDB,1,PHP,14,PHP Framework,6,PHP MYSQLI,9,PHP OOP,5,Python,8,Python 3,4,SQL Server,4,SQLite,4,Uncategorized,5,Vb 6,2,Vb.Net,89,Video,48,Vue Js,4,WPF,2,Yii,3,
ltr
item
KODE AJAIB: Laravel 5 Tutorial : Create Multi Databases Connection in laravel 5.3
Laravel 5 Tutorial : Create Multi Databases Connection in laravel 5.3
Laravel 5.3 Tutorial for beginners : How to create database connection in Laravel 5.3? make multi connection to MySQL database in laravel.
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgZZQ_sZE-wy5Lq8-Xm805USbPGdgvn9omvXV7sBd_vmrypxqso_tsZzucyTdeX2boXifiEm-DjGlTjshkrnO7bDcPC3rIMkpxUj9yZnoMej9x5zjKJtVgc4HAq-f2pMQ9vA7ZeFZ2aPx4/s320/3.png
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgZZQ_sZE-wy5Lq8-Xm805USbPGdgvn9omvXV7sBd_vmrypxqso_tsZzucyTdeX2boXifiEm-DjGlTjshkrnO7bDcPC3rIMkpxUj9yZnoMej9x5zjKJtVgc4HAq-f2pMQ9vA7ZeFZ2aPx4/s72-c/3.png
KODE AJAIB
https://scqq.blogspot.com/2016/10/laravel-5-create-multi-database-connection.html
https://scqq.blogspot.com/
https://scqq.blogspot.com/
https://scqq.blogspot.com/2016/10/laravel-5-create-multi-database-connection.html
true
3214704946184383982
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy