If you want to set Laravel DB to be SQL Server, you need to edit .env file and add sqlsrv as your DB connection
DB_CONNECTION=sqlsrv
DB_HOST=127.0.0.1
DB_PORT=1433
DB_DATABASE=YOUR_DB_NAME
DB_USERNAMEYOUR_USERNAME
DB_PASSWORD=YOUR_PASSWORD
You need to save the .env file and clear the config cache
php artisan config:clear
Also you need to download Microsoft Driver for PHP for SQLServer from this url:
Extract the downloaded file and copy the files that match your PHP version into the ext folder of your php folder
After that, you need to edit the php.ini (in your PHP folder) to add the drivers you just downloaded
extension=php_sqlsrv_82_ts_x64.dll
extension=php_pdo_sqlsrv_82_ts_x64.dll
Note that you have to edit the names of the extensions to match the driver files you just downloaded which match with PHP version, in my case it was 8.2 version
After saving the php.ini you must restart the apache server for changes to take effect
Now if you want to test the DB connection , you can open a terminal and go to tinker
php artisan tinker
and then type
DB::connection()->getDatabaseName()
If you get the name of the DB, then the connection is good, but wait .. that doesn’t mean you are able to read or write from the DB. For example, if you tried to do a migrate and you get an error like this:
SQLSTATE[08001]: [Microsoft][ODBC Driver 17 for SQL Server]TCP Provider: The wait operation timed out.
(Connection: sqlsrv, SQL: select * from sys.sysobjects where id = object_id(migrations) and xtype in (‘U’, ‘V’))
That means there is most probably a problem with the credentials or the port used
First, you need to make sure that the user has SQL Server authentication, not Windows authentication, you need to open SQL Server Management Studio and right on the Server from the left menu, click on Properties and then Security and choose SQL Server and Windows Authentication mode option
After clicking save, go to the left menu: Security > Login and right click on the user name and click on Properties and make sure that the option of SQL Server Authentication is active
Better to restart the SQL Server by right click on the Server name from the left menu and click on Restart.
If you still get an error message, try to stop the SQL server Process from Task manager