0

I have several DB configured on my Laravel app. Some are sqlsrv which works correctly. The default DB is a MYSQL DB. Also I have two more mysql db configured. When I try to query any of this two DB I get an error

When I do

   $bookings = DB::connection('crm')
            ->table('act_bookings')
            ->where('deleted','=',0)
            ->get();

I get the error SQLSTATE[42S02]: Base table or view not found: 1146 Table 'wscrm.act_bookings' wscrm is the name of the default DB.

LAravel not even try to connect to DB just throw this error. LAravel Version 6.2

Miquel Àngel
  • 149
  • 1
  • 3
  • 11

4 Answers4

2

Your connection name should be in config/database. php Example => 'connections' => [

    'crm' => [
        'driver' => 'mysql',
        'host' => env('DB_HOST', '127.0.0.1'),
        'port' => env('DB_PORT', '3306'),
        'database' => env('DB_TEST_DATABASE', 'testing'),
        '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' => 'InnoDB ROW_FORMAT=DYNAMIC',
    ],
kablanfatih
  • 473
  • 4
  • 14
2

Your connection should be defined in config/database.php like

        'mysql' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'default'),
            'username' => env('DB_USERNAME', 'default'),
            'password' => env('DB_PASSWORD', ''),
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'prefix_indexes' => true,
            'strict' => true,
            'engine' => 'InnoDB ROW_FORMAT=DYNAMIC',
        ],
        'crm' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'default'),
            'username' => env('DB_USERNAME', 'default'),
            'password' => env('DB_PASSWORD', ''),
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'prefix_indexes' => true,
            'strict' => true,
            'engine' => 'InnoDB ROW_FORMAT=DYNAMIC',
        ],
ugurdnlr
  • 74
  • 2
0

try this =

$bookings = DB::connection('crm')
            ->table('act_bookings')
            ->where('deleted','=',0)
            ->get();

Make sure the database, username & password are configured in .env

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=crm
DB_USERNAME=root
DB_PASSWORD=
Tammam
  • 416
  • 1
  • 7
  • 16
  • The default DB which name is wscrm are already in .env file. I'm trying to connect to another one that I configured like this in database.php 'crm' => [ 'driver' => 'mysql', 'host' => env('DB_HOST', '127.0.0.1'), 'port' => env('DB_PORT', '3306'), 'database' => 'crm', 'username' => 'dbusername', 'password' => 'dbpassword', .... – Miquel Àngel Feb 21 '20 at 08:21
  • whether it has been tried in this way?? https://stackoverflow.com/questions/31847054/how-to-use-multiple-databases-in-laravel – Tammam Feb 21 '20 at 10:08
0

Finally it was my mistake, a very stupid one.

I defined the connection in database.php But in this line: 'database' => env('DB_TEST_DATABASE', 'testdb') I left the env() instead this: 'database' => 'testdb'

Miquel Àngel
  • 149
  • 1
  • 3
  • 11