How to connect MSSQL Server Database in Laravel 9

How to connect MSSQL Server Database in Laravel 9

K. M. Shawkat Zamil | December 14, 2022

Hello Developers,

In this demonstration, I will show you how you connect your Laravel 9 project with MSSQL Server. You all know very well that Laravel 9 supports a minimum of PHP v8.0 version. For that, our apache server needs to configure with the MSSQL driver. I will show the steps easily.


First Step: Install the XAMPP (for apache)


For apache distribution I will use XAMPP, you can also use WAMPP or any other apache distribution software. (XAMPP download link). After downloading, we will find the D:/xampp/htdocs folder. Here are all the projects stored. 


Second Step: Configure XAMPP with the MSSQL driver


For that, go to the Microsoft official site and download the driver zip file. (Driver download link). After downloading, extract the file. As our Xampp PHP version in v 8.0, that's why we only take the following two driver file and paste them D:/xampp/php/ext/





After pasting the files into that directory, open the php.ini file and add the below two lines.





After adding the lines start the xampp apache server.


Third Step: Install Laravel 9


Now come to the main part. Installation of Laravel 9 in D:/xampp/htdocs folder directory. Before that, you need to download the Composer



composer global require laravel/installer
laravel new example-app


After installation, go to the /.env file and configure the Laravel project with mssql server. Find the below block:





Replace the block with the below code:




To connect with MSSQL, we need to put sqlsrv in the DB_CONNECTION, set the PORT 1433, The other field will be completed by the user. To check the database is correctly connected, just put the below code in  the initial route:


// Test database connection
try {
} catch (\Exception $e) {
    die("Could not connect to the database.  Please check your configuration. error:" . $e );


Run the project, with the below command:


php artisan serve


If everything is connected properly, you will get a blank page. 


Hope this might help in the journey of development.


Read More: How to excel import with date format in Laravel


K. M. Shawkat Zamil

K. M. Shawkat Zamil

Senior Software Engineer

I am a Senior Software Engineer in a reputed company in Bangladesh. I am a big fan of Laravel, PHP Programming, MSSQL Server, MySql, JavaScript, and lots more. I love to help people who are especially eager to learn. I believe in patience and motivation.