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

shanely's avatar

configuration SQL SERVER

Hi, can I ask some help I am connecting my laravel to SQL SERVER and I don't if this is the correct way. here is my set up , in database.php I add this

 'sqlsrv' => [
            'driver' => 'sqlsrv',
            'host' => 'localhost',
            'database' => 'database',
            'username' => 'root',
            'password' => '',
            'prefix' => '',
        ],

and I change this from

'default' => env('DB_CONNECTION', 'mysql'),

to

'default' => env('DB_CONNECTION', 'sqlsrv'),

then in my .env file

DB_CONNECTION=sqlsrv
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=mydbserver
DB_USERNAME=john
DB_PASSWORD=john123

Thank you in advance

0 likes
5 replies
spekkionu's avatar

You need to actually use the environment variables if you want them to do anything.

'sqlsrv' => [
            'driver'   => 'sqlsrv',
            'host'     => env('DB_HOST', 'localhost'),
            'database' => env('DB_DATABASE', 'database'),
            'username' => env('DB_USERNAME', 'root'),
            'password' => env('DB_PASSWORD', ''),
            'charset'  => 'utf8',
            'prefix'   => '',
],

Then it should use the ones set in your .env file.

shanely's avatar

@spekkion,

Thank you for correcting me. also can I ask is my env is correct especially the port ?

Thank you in advance.

spekkionu's avatar

That is the default port for mysql.
The default port for sql server is 1433.

You only need to provide the port if it is different than the default.
You'll also need to add it to the config file for it to be used.

shanely's avatar

@spekkionu ,

what do you mean by this

You'll also need to add it to the config file for it to be used. ?

you mean add port in the database.php ? like this

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

in my .env

DB_PORT=1433
giacomopiva's avatar

Hello, I'm getting the same error and I can't solve it. How did you get the connection working?

Please or to participate in this conversation.