Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Using multiple database connections inside controllers

I have setup multiple database connections

"wc" => [
            "driver" => "mysql",
            "url" => env("DATABASE_URL"),
            "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" => "utf8mb4",
            "collation" => "utf8mb4_unicode_ci",
            "prefix" => "",
            "prefix_indexes" => true,
            "strict" => true,
            "engine" => null,
            "options" => extension_loaded("pdo_mysql")
                ? array_filter([
                    PDO::MYSQL_ATTR_SSL_CA => env("MYSQL_ATTR_SSL_CA"),
                ])
                : [],
        ],

        "ys" => [
            "driver" => "mysql",
            "url" => env("DATABASE_URL"),
            "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" => "utf8mb4",
            "collation" => "utf8mb4_unicode_ci",
            "prefix" => "",
            "prefix_indexes" => true,
            "strict" => true,
            "engine" => null,
            "options" => extension_loaded("pdo_mysql")
                ? array_filter([
                    PDO::MYSQL_ATTR_SSL_CA => env("MYSQL_ATTR_SSL_CA"),
                ])
                : [],
        ],


DB_CONNECTION=wc
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=wc
DB_USERNAME=root
DB_PASSWORD=

DB_CONNECTION=ys
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=ys
DB_USERNAME=root
DB_PASSWORD=

and i would like to choose which conenction to use when running a controller

<?php

namespace App\Http\Controllers\adx_world;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

use DB;
use App\Http\Requests;

class ApiController extends Controller
{

    public function creator(){
       
     $data=array('name'=>time(),"city"=>time());
     DB::connection('ys')->table('test')->insert($data);
     echo "Record inserted successfully.<br/>";
    
    }
}

This throws an error

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

SQLSTATE[42S02]: Base table or view not found:

What could be the problem?

>Solution :

Your configuration is incorrect. You are using same env variables for both connections, which means that you’re overwriting it in .env file. So you need unique keys in env files which are used in 2 connections.

Change to this your config and env:

"wc" => [
        "driver" => "mysql",
        "url" => env("DATABASE_URL"),
        "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" => "utf8mb4",
        "collation" => "utf8mb4_unicode_ci",
        "prefix" => "",
        "prefix_indexes" => true,
        "strict" => true,
        "engine" => null,
        "options" => extension_loaded("pdo_mysql")
            ? array_filter([
                PDO::MYSQL_ATTR_SSL_CA => env("MYSQL_ATTR_SSL_CA"),
            ])
            : [],
    ],

    "ys" => [
        "driver" => "mysql",
        "url" => env("YS_DATABASE_URL"),
        "host" => env("YS_DB_HOST", "127.0.0.1"),
        "port" => env("YS_DB_PORT", "3306"),
        "database" => env("YS_DB_DATABASE", "forge"),
        "username" => env("YS_DB_USERNAME", "forge"),
        "password" => env("YS_DB_PASSWORD", ""),
        "unix_socket" => env("DB_SOCKET", ""),
        "charset" => "utf8mb4",
        "collation" => "utf8mb4_unicode_ci",
        "prefix" => "",
        "prefix_indexes" => true,
        "strict" => true,
        "engine" => null,
        "options" => extension_loaded("pdo_mysql")
            ? array_filter([
                PDO::MYSQL_ATTR_SSL_CA => env("MYSQL_ATTR_SSL_CA"),
            ])
            : [],
    ],


DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=wc
DB_USERNAME=root
DB_PASSWORD=

YS_DB_CONNECTION=mysql
YS_DB_HOST=127.0.0.1
YS_DB_PORT=3306
YS_DB_DATABASE=ys
YS_DB_USERNAME=root
YS_DB_PASSWORD=
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading