Search code examples
phplaravellaravel-9sqlsrvlaragon

Laravel 9 (sqlsrv) could not find driver


I have this issue with trying to connect MS SQL server from laravel project to get some data from table MECommssion I have created the command name test.php in the handle function of the below code

     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $crmData = DB::connection('core')->table('dbo.MECommssion')->get(); 
        dd($crmData);
    }

In config/database.php

 'core' => [
            'driver'    => env('DB_CONNECTION_SYNTELLICORE'),
            'host'      => env('DB_HOST_SYNTELLICORE'),
            'port'      => env('DB_PORT_SYNTELLICORE'),
            'database'  => env('DB_DATABASE_SYNTELLICORE'),
            'username'  => env('DB_USERNAME_SYNTELLICORE'),
            'password'  => env('DB_PASSWORD_SYNTELLICORE'),
            'charset' => 'utf8',
            'prefix' => '',
            'prefix_indexes' => true,
        ],

In .env I have add the login

DB_CONNECTION_SYNTELLICORE=sqlsrv
DB_HOST_SYNTELLICORE=192.168.4.59
DB_PORT_SYNTELLICORE=1433
DB_DATABASE_SYNTELLICORE=test
DB_USERNAME_SYNTELLICORE=MSSB
DB_PASSWORD_SYNTELLICORE=mypassword

Every time I call the command I got this error in terminal

Illuminate\Database\QueryException 

  could not find driver (SQL: select * from [dbo].[MECommssion])

  at E:\laragon\www\InfoPortal\Backoffice\vendor\laravel\framework\src\Illuminate\Database\Connection.php:760
    756▕         // If an exception occurs when attempting to run a query, we'll format the error
    757▕         // message to include the bindings with SQL, which will make this exception a
    758▕         // lot more helpful to the developer instead of just the database's errors.
    759▕         catch (Exception $e) {
  ➜ 760▕             throw new QueryException(
    761▕                 $query, $this->prepareBindings($bindings), $e
    762▕             );
    763▕         }
    764▕     }

  1   E:\laragon\www\InfoPortal\Backoffice\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php:70
      PDOException::("could not find driver")

  2   E:\laragon\www\InfoPortal\Backoffice\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php:70
      PDO::__construct("dblib:host=192.168.4.59:1433;dbname=test;charset=utf8", "MSSB", "mypassword", [])

What I trying to do is

1- install ODBC drivers 11, 17 and 18.

2- adding DLL extension for PHP 8.1.10php_pdo_sqlsrv_81_ts_x64 and php_sqlsrv_81_ts_x64.dll and enable the extension from php.ini. enter image description here

3- restart the server.

4- restart the machine.

5- For every change in .env I re-clear the catch.

6- trying the connection in a separate PHP file use PDO out of laravel project below code it is working with no problem and it shows data.

<?php
$servername = "192.168.4.59,1433";
$username = "MSSB";
$password = "mypassword";

try {
    $conn = new PDO("sqlsrv:Server={$servername};Database=test", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        
        $stmt = $conn->query("SELECT * FROM dbo.MECommssion");
        $user = $stmt->fetch();
         print_r($user);
    
    echo "Connected successfully";
    }
catch(PDOException $e)
    {
    echo "Connection failed: ". $e->getMessage();
    }

7- install laragon server on another machine and install the new laravel project same version 9 and create the same command and try the same thing in the new project it is working normally with no problem.

8- composer dump autoload

This is all that I tried, I have no idea why the project did not read the drive sqlsrv while the separate PHP file can connect and get the data.

I would appreciate any help you can provide.


Solution

  • Thanks for help.

    I have found the problem it was in the composer PHP version was running PHP in the WAMP server on the same machine, after removing the WAMP server and re-install the composer in laragon server in PHP 8.1.10 folder, Now all working fine