'Laravel MSSQL Server Connection not working
Laravel version:7.13, SqlServer:2019
 SQLSTATE[08001]: [Microsoft][ODBC Driver 17 for SQL Server]TCP Provider: 
 An existing connection was forcibly closed by the remote host (SQL: select * from [users]).
Above is error message what I got.
I tried with core php and it works well.
Below is php code.
<?php
$serverName = "MYPCNAME";
$connectionInfo = array( "Database"=>"DBNAME", "UID"=>"sa", "PWD"=>'MyPassword');
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn ) {
     echo "Connection established.<br />";
     if(($result = sqlsrv_query($conn,"SELECT * FROM users")) !== false){
        while( $obj = sqlsrv_fetch_object( $result )) {
              echo $obj->name.'<br />';
        }
    }
}else{
     echo "Connection could not be established.<br />";
     die( print_r( sqlsrv_errors(), true));
}
?>
I think SQL Server configuration is correct. I installed php_sqlsrv, php_pdo_sqlsrv extensions and they are displayed as installed in phpinfo()
Here is my Laravel configuration.
DB_CONNECTION=sqlsrv
DB_HOST=MYPCNAME
DB_PORT=1433
DB_DATABASE=DBNAME
DB_USERNAME=sa
DB_PASSWORD=MyPassword
Following is database config file.
 'sqlsrv' => [
            'driver' => 'sqlsrv',
            'url' => env('DATABASE_URL'),
            'host' => env('DB_HOST'),
            'port' => env('DB_PORT'),
            'database' => env('DB_DATABASE'),
            'username' => env('DB_USERNAME'),
            'password' => env('DB_PASSWORD'),
            'charset' => 'utf8',
            'prefix' => '',
            'prefix_indexes' => true,
        ],
I tried to change DB_HOST with 127.0.0.1 too but there was same issue.
I was going to get users from users table. 
My thought: I changed server name with 127.0.0.1 in core php and it displayed exact same error with laravel.
Can anyone help me? Thanks
Solution 1:[1]
Ok, I sovled issue.
When I set DB_PORT=null it worked.
However, I didn't find the exact answer yet.
Here is post url about it.
PHP Sqlsrv connection not working when specify port, instance
I think the main reason is above.
Anyway If I set DB_PORT=null, it worked successfully.
Solution 2:[2]
Sometimes you just run this command on the terminal. Note that if it was working perfectly before the error
php artisan optimize:clear
    					Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source | 
|---|---|
| Solution 1 | lovecoding | 
| Solution 2 | ephantus okumu | 
