support@codebucket.net

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/

 

D:/xampp/php/ext/

php_sqlsrv_81_ts_x64.dll
php_pdo_sqlsrv_81_ts_x64.dll

 

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

 

D:/xampp/php/php.ini

extension=php_sqlsrv_81_ts_x64.dll
extension=php_pdo_sqlsrv_81_ts_x64.dll

 

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

 

D:/xampp/htdocs/cmd.exe

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:

 

/example-app/.env

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=test
DB_USERNAME=root
DB_PASSWORD=

 

Replace the block with the below code:

 

DB_CONNECTION=sqlsrv
DB_HOST=[USER_HOST]
DB_PORT=1433
DB_DATABASE=[USER_DATABASE]
DB_USERNAME=[USER_USERNAME]
DB_PASSWORD=[USER_PASSWORD]

 

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 {
    DB::connection()->getPdo();
} 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.