Odunayo Ogungbure
Posted on August 16, 2019
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.
laravel new mssql
Open the database.php file in the config folder and make this change.
/*
|--------------------------------------------------------------------------
| 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
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.
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).
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 or this GitHub page. 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
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
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
extension=php_pdo_sqlsrv_7x_ts.dll
extension=php_sqlsrv_7x_ts.dll
- Restart xamp server.
Run the migration command in our terminal.
php artisan migrate
Now we should be able to run our migrations.
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.
UPDATE: You might need to download Microsoft ODBC Driver 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 ๐
Posted on August 16, 2019
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.