Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Smsma's avatar
Level 2

connect to multiple database type

can I connect to different type of database like mysq and sqlserv in one laravel project at the same time, and if I can how ?!

0 likes
6 replies
Smsma's avatar
Level 2

in .env file how can I make this connection ?

Smsma's avatar
Level 2

is this right?!

//mySQl

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret
//sqlsrv
DB_CONNECTION_SECOND=sqlsrv
DB_HOST_SECOND=127.0.0.1
DB_PORT_SECOND=1433
DB_DATABASE_SECOND=homestead2
DB_USERNAME_SECOND=homestead
DB_PASSWORD_SECOND=secret

kayschima's avatar

If you want to set your parameters that way you still have to change your config/database.php-file .

1 like
Borisu's avatar
Borisu
Best Answer
Level 37
  1. Go to config/database.php
  2. Add your database connection
'connections' => [
    'sqlsrv' => [
        'driver' => 'sqlsrv',
            'host' => env('DB_HOST_SECOND', 'localhost'),
            'port' => env('DB_PORT_SECOND', '1433'),
            'database' => env('DB_DATABASE_SECOND', 'forge'),
            'username' => env('DB_USERNAME_SECOND', 'forge'),
            'password' => env('DB_PASSWORD_SECOND', ''),
            'charset' => 'utf8',
            'prefix' => '',
    ],
]
  1. Setup your env file
  2. Use as per documentation.

Please or to participate in this conversation.