Share and Learn To Write Code!

Programming articles, which help you to build your application. If something you are stuck to code and complete the program, just find here your question-related articles.

Share with   

Setting Multiple Database Connection In Lumen - PHP Micro-Framework By Laravel

Setting Multiple Database Connection In Lumen - PHP Micro-Framework By Laravel
php   lumen php   laravel   php api  

Lumen - PHP Micro-Framework


Laravel Lumen is a stunningly fast PHP micro-framework for building web applications with an expressive, elegant syntax. Lumen attempts to take the pain out of development by easing common tasks used in the majority of web projects, such as routing, database abstraction, queueing, and caching.


Getting Started with Laravel Lumen follow this article.


Here we are going to connect multiple database connection for making queries from multiple DB's.


Installation Steps


Open .env file from project directory where your database connection is stored.


Add your database connection to that file like bellow code in .env file.


APP_NAME=Lumen
APP_ENV=local
APP_KEY=
APP_DEBUG=true
APP_URL=http://localhost
APP_TIMEZONE=UTC

LOG_CHANNEL=stack
LOG_SLACK_WEBHOOK_URL=

// 1st Databse Connection 
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=Blogs
DB_USERNAME=root
DB_PASSWORD=blogs290

// 2nd Databse Connection 
DB_CONNECTION_SECOND=mysql
DB_HOST_SECOND=127.0.0.1
DB_PORT_SECOND=3306
DB_DATABASE_SECOND=Profiles
DB_USERNAME_SECOND=root
DB_PASSWORD_SECOND=profile980

CACHE_DRIVER=file
QUEUE_CONNECTION=sync


Here I am adding two database connection named as Blogs Database and Profiles Database.


Now register this another 2nd database connection to database config file.


Go to project > vendor > laravel > lumen-framework > config > database.php File


Add your 2nd database connection to connections property


Example


'connections' => [


        'sqlite' => [
            'driver' => 'sqlite',
            'database' => env('DB_DATABASE', database_path('database.sqlite')),
            'prefix' => env('DB_PREFIX'''),
        ],


        'mysql' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST''127.0.0.1'),
            'port' => env('DB_PORT'3306),
            'database' => env('DB_DATABASE''forge'),
            'username' => env('DB_USERNAME''forge'),
            'password' => env('DB_PASSWORD'''),
            'unix_socket' => env('DB_SOCKET'''),
            'charset' => env('DB_CHARSET''utf8mb4'),
            'collation' => env('DB_COLLATION''utf8mb4_unicode_ci'),
            'prefix' => env('DB_PREFIX'''),
            'strict' => env('DB_STRICT_MODE'true),
            'engine' => env('DB_ENGINE'null),
            'timezone' => env('DB_TIMEZONE''+00:00'),
        ],


        'mysql2' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST_SECOND''127.0.0.1'),
            'port' => env('DB_PORT_SECOND'3306),
            'database' => env('DB_DATABASE_SECOND''forge'),
            'username' => env('DB_USERNAME_SECOND''forge'),
            'password' => env('DB_PASSWORD_SECOND'''),
            'unix_socket' => env('DB_SOCKET'''),
            'charset' => env('DB_CHARSET''utf8mb4'),
            'collation' => env('DB_COLLATION''utf8mb4_unicode_ci'),
            'prefix' => env('DB_PREFIX'''),
            'strict' => env('DB_STRICT_MODE'true),
            'engine' => env('DB_ENGINE'null),
            'timezone' => env('DB_TIMEZONE''+00:00'),
        ],


        'pgsql' => [
            'driver' => 'pgsql',
            'host' => env('DB_HOST''127.0.0.1'),
            'port' => env('DB_PORT'5432),
            'database' => env('DB_DATABASE''forge'),
            'username' => env('DB_USERNAME''forge'),
            'password' => env('DB_PASSWORD'''),
            'charset' => env('DB_CHARSET''utf8'),
            'prefix' => env('DB_PREFIX'''),
            'schema' => env('DB_SCHEMA''public'),
            'sslmode' => env('DB_SSL_MODE''prefer'),
        ],


        'sqlsrv' => [
            'driver' => 'sqlsrv',
            'host' => env('DB_HOST''localhost'),
            'port' => env('DB_PORT'1433),
            'database' => env('DB_DATABASE''forge'),
            'username' => env('DB_USERNAME''forge'),
            'password' => env('DB_PASSWORD'''),
            'charset' => env('DB_CHARSET''utf8'),
            'prefix' => env('DB_PREFIX'''),
        ],


    ],


You can Now you can add multiple connection to your lumen project by adding property to database.php config file. Here i am register mysql2 into connection property


You need to turn on fascade to access another database connection.


Go to  project > bootstrap > app.php and just add the bellow code. If you already turn on fascade avoid this.

$app->withFacades();


Now create directory for get your database connections to your controllers and register to app.php file. Example


Create Directory project > configurations > db_config.php

Add the bellow code to db_config.php file and change the names you like.

<?php
    use Illuminate\Support\Facades\DB;
    class DBController
    {
        public function db1(){
            return app('db');
        }

        public function db2(){
            return DB::connection('mysql2');
        }
    }
?>


Here app('db') returns your first database connection. Becausee it set as by default.

But when you need to get another database connection you need use fascade so you can access registered your another database from database.php config file


We will create two functions which returns database connections accessibility to controller or route or any other class.


For accessing another database use the library

use Illuminate\Support\Facades\DB;

And for getting another database use the fascade like this

DB::connection('conection_name');

connection_name is name which is written in database.php config file


Register DBController into project > bootstrap > app.php just add file to require_once like to use in your controllers

require_once __DIR__.'/../configuration/db_config.php';


Example to use the dbcontroller into your class controller

0replies
Leave a reply
46
Registerd Users
87
Total Subscribers
59
Total Articles
38917
Total Views