How to Use SQL Server Connection in Laravel

How to use SQL Server connection in Laravel?

Ahh after a lot more research, I found out that my driver of SQL Server Native Client 10.0 was out of date and not sufficient to connect to a MsSQL Server 2008 with PDO, you need to have at least version 11.0 in order to connect to an SQL Server 2005+

If you want to validate your driver version, you can go through Control Panel >> Administrative Tools >> Data Sources (ODBC) and then click on the Drivers tab to find out which SQL Server Native Client you have already installed. If you want to update your driver, depending on your OS configuration you may choose the proper link below...

SQL Server 2012 Native Client

x86 (32 bits) Package http://go.microsoft.com/fwlink/?LinkID=239647&clcid=0x409

x64 (64 bits) Package http://go.microsoft.com/fwlink/?LinkID=239648&clcid=0x409

Connecting SQL Server to laravel as a second database

Change all the DB_DATABASE from your config file to DB_EXT_DATABASE, as you wrote in your .env file.

Try instead of

$users = DB::connection('sqlsrv')->table('dbo.t_people')->select('*')->get();

do the

$users = DB::connection('sqlsrv')->table('t_people')->select('*')->get();

And don't do it in your blade files, do it in controller.

Laravel and MS SQL Server Database connection is throwing the error (3/3) QueryException could not find driver

So I was finally able to figure out the issue, Posting the same so it will help someone who has struck for days and ripping their hair out to find the solution.

  1. Make sure you have given proper port in the Laravel while connection and its same as the one SQL Server Configuration Manager TCP/IP.

  2. Make sure your TCP/IP is Enabled in SQL Server Configuration Manager.

  3. I had some Extension which were missing in my php.ini file so I copy pasted these extension on to my php.ini file in the PHP of WAMP.

    extension=php_bz2.dll

    extension=php_curl.dll

    extension=php_sqlsrv_56_ts.dll

    extension=php_sqlsrv_56_nts.dll

    extension=php_com_dotnet.dll

    ;extension=php_enchant.dll

    extension=php_fileinfo.dll

    ;extension=php_ftp.dll

    extension=php_gd2.dll

    extension=php_gettext.dll

    extension=php_gmp.dll

    extension=php_intl.dll

    extension=php_imap.dll

    ;extension=php_interbase.dll

    extension=php_ldap.dll

    extension=php_mbstring.dll

    extension=php_exif.dll ; Must be after mbstring as it depends on it

    extension=php_mysqli.dll

    extension=php_odbc.dll

    extension=php_openssl.dll

    ;extension=php_pdo_firebird.dll

    extension=php_pdo_mysql.dll

    ;extension=php_pdo_oci.dll

    ;extension=php_oci8_12c.dll ; Use with Oracle Database 12c Instant Client

    extension=php_pdo_odbc.dll

    ;extension=php_pdo_pgsql.dll

    extension=php_pdo_sqlite.dll

    ;extension=php_pgsql.dll

    ;extension=php_phpdbg_webhelper.dll

    ;extension=php_shmop.dll

  4. Every time you change something in the .env file in the Laravel, you need to refresh it otherwise it will still use the old data which you have provided.

    php artisan config:clear

    php artisan config:cache

  5. Restart the WAMP Server and the Laravel server php artisan server

  6. My .env file in the Laravel:

    DB_CONNECTION=sqlsrv

    DB_HOST=127.0.0.1

    DB_PORT=1433

    DB_DATABASE=DataBase_Name

    DB_USERNAME=sa

    DB_PASSWORD=MyPass

Hopefully this should work for you guys, Otherwise all the best and try to find the resolution and please let me also know.

Connecting Laravel with SQL Server database always shows error could not find driver

So this all happened because of the detected different versions of PHP and SQL Server drivers.

For information, I have 3 local servers, namely:

  1. XAMPP 1.7.3,
  2. 3,, (I forgot the version details),
  3. and Laragon.

The problem that occurs is, when I want to develop apps using the Laragon server AND I use the local Terminal/Command Prompt (Shortcut: Windows + CMD), the PHP versions from other local servers will collide with each other.

When I run a Laravel command, check the PHP version, or whatever, the local Command Prompt directs me to the version of XAMPP above that I have installed, so any .dll or plugins you install in Laragon won't be detected!

Finally I was able to finish my installation and do a lot of CMD commands according to the version I want, is how to use the built-in CMD/Terminal from Laragon (Click on Terminal under Laragon application)

From this Terminal, all the .dll plugins that you install, and all the cmd commands that you enter, will match the Laragon version you are using, they won't be strayed to another local server! :D



Related Topics



Leave a reply



Submit