Setting up a Laravel project with SQL Server AND XAMPP / WAMP on Windows
Tags: laravel, mssql, windows
Published: 2019-08-16 11:47:03

I spent a large part of yesterday trying to connect an existing Laravel project to MsSQL. I made some mistakes and used different tutorials but I finally got it setup.

##### **Note:** *This article assumes you have SQL Server, xampp or wamp and laravel correctly installed on your system.*

First, we create a fresh Laravel installation and when it's complete we can open the folder in our editor. 
```php
laravel new mssql
```
Open the database.php file in the config folder and make this change.
```php
 /*
    |--------------------------------------------------------------------------
    | Default Database Connection Name
    |--------------------------------------------------------------------------
    |
    | Here you may specify which of the database connections below you wish
    | to use as your default connection for all database work. Of course
    | you may use many connections at once using the Database library.
    |
    */
    //from
    'default' => env('DB_CONNECTION', 'mysql'),
    //to
    'default' => env('DB_CONNECTION', 'sqlsrv'),
```
This is not really important as we are still going to update the .env file, but hey I just feel like doing it :).

Next, we need to update our .env file database credentials
```php
DB_CONNECTION=sqlsrv
DB_HOST=127.0.0.1
DB_PORT=1433
DB_DATABASE=laravel
DB_USERNAME= #SQL Server username
DB_PASSWORD= #SQL Server password
```
Let's run our migrations using the migrate artisan command in our terminal.
```ps
 php artisan migrate
```
We should get the error below. This means we need to download Microsoft drivers for PHP for SQL Server. *(If your migrations did run successfully, then there's no further setup to do)*. 
```php
 Illuminate\Database\QueryException: could not find driver 
(SQL: select * from sysobjects where type = 'U' and name = migrations)
```
You can download the appropriate drivers from [Microsoft store](https://www.microsoft.com/en-us/download/details.aspx?id=20098) or [this GitHub page](https://github.com/Microsoft/msphpsql/releases). I used the latter so I can easily download the drivers for my PHP version(Windows-7.2.zip). *(I'm using PHP 7.2)*.

Extract the contents of the zip file and copy the php_pdo_sqlsrv_7x_ts.dll and php_sqlsrv_7x_ts.dll file.

**On WAMPP**
- Paste the files in C:\wamp64\bin\php\php7.x\ext
- Open the php.ini file in C:\wamp64\bin\php\php7.x folder and add the following lines
```php
 extension=php_pdo_sqlsrv_7x_ts.dll
 extension=php_sqlsrv_7x_ts.dll
```
- Open the php.ini file in C:\wamp64\bin\apache\apache2.4.xx\bin folder and add the following lines
```php
 extension=php_pdo_sqlsrv_7x_ts.dll
 extension=php_sqlsrv_7x_ts.dll
```
- Restart wampp server.

**On XAMP**
- Paste the files in C:\xampp\php\ext
- Open the php.ini file in C:\xampp\php folder and add the following lines
```php
 extension=php_pdo_sqlsrv_7x_ts.dll
 extension=php_sqlsrv_7x_ts.dll
```
- Restart xamp server.

Run the migration command in our terminal.
```ps
 php artisan migrate
```
Now we should be able to run our migrations. 
```php
Migrating: 2014_10_12_000000_create_users_table
Migrated:  2014_10_12_000000_create_users_table (0.01 seconds)
Migrating: 2014_10_12_100000_create_password_resets_table
Migrated:  2014_10_12_100000_create_password_resets_table (0.01 seconds)
```

**_Bonus:_** *(I'm on SQL Server 2012 by the way) Using `unsignedBigInteger()` will throw an error, instead use `bigInteger()`. More on this [here](https://laraveldaily.com/be-careful-laravel-5-8-added-bigincrements-as-defaults/).*

**_UPDATE:_** *You might need to download [Microsoft ODBC Driver](https://go.microsoft.com/fwlink/?LinkId=163712) to fix this - Illuminate\Database\QueryException  : SQLSTATE[IMSSP]: This extension requires the Microsoft ODBC Driver for SQL Server to communicate with SQL Server. Access the following URL to download the ODBC Driver for SQL Server for x64: https://go.microsoft.com/fwlink/?LinkId=163712*

**That’s All Folks!
Happy Coding 🖖**