0

I use several databases including a SQL Server database

DB::connection('sqlsrv')->from('my_table')->get();

In my model, if I use the classical method from a controller it works. But if I call this same method from a custom command, I get an error message that the drivers are not found.

php artisan test:example

When executing the query, I got the following error message

could not find driver (SQL:

I also noticed that from my custom command, the database connection specified in the template was not taken into account.

protected $connection = 'sqlsrv';

The problem appeared since the migration from version 8 to version 9.

On the same server, I have another site running with the same drivers and also using custom commands. He too was migrated from version 8 to version 9 and everything works perfectly. Locally it works very well.

The behavior is really strange, thanks in advance for your help.

I tried clearing the Laravel cache but still having the same problem.

My database configuration (.env)

DB_CONNECTION_MSSQL=sqlsrv
DB_HOST_MSSQL=192.1.1.10
DB_PORT_MSSQL=1433
DB_DATABASE_MSSQL=dbV3
DB_USERNAME_MSSQL=web
DB_PASSWORD_MSSQL=secret

In the config/database.php file

'sqlsrv' => [
            'driver' => 'sqlsrv',
            /*'url' => env('DATABASE_URL'),*/
            'host'     => env('DB_HOST_MSSQL', 'localhost'),
            'port'     => env('DB_PORT_MSSQL', '1433'),
            'database' => env('DB_DATABASE_MSSQL', 'forge'),
            'username' => env('DB_USERNAME_MSSQL', 'forge'),
            'password' => env('DB_PASSWORD_MSSQL', ''),
            /*'charset' => 'utf8',
            'prefix' => '',
            'prefix_indexes' => true,*/
        ],

In the model

protected $connection = 'sqlsrv';

Same problem with both ways

DB::connection('sqlsrv')->from('my_table')->get();

Or

$query = static::all();
Damien V
  • 1
  • 3
  • 1
    You need to show your database config for us to debug. Be sure to hide sensitive information. – amac Mar 29 '22 at 08:11
  • purging the `vendor` folder and reinstalling everything is also an option if you did inplace upgrade. also, make sure [every packages](https://stackoverflow.com/questions/42557693/laravel-pdoexception-could-not-find-driver) are installed properly. please dont truncate the error message, only censor the query. – Bagus Tesa Mar 30 '22 at 13:21
  • @BagusTesa Thanks for your help. I just deleted the vendor folder, after a new composer install the problem is still there. I have the impression that the command does not find the sqlsrv drivers while everything is ok in my methods. Maybe I need to restart a serice on the server? – Damien V Apr 13 '22 at 14:06
  • @AlexMac I just updated the post with the configuration – Damien V Apr 13 '22 at 14:07
  • @DamienV, is your php instance has sql server driver enabled (check on `php.ini`, see this [qa](https://stackoverflow.com/a/55134712))? i assumed what was changed just the laravel instance not the environment. guess i'm wrong. just to be sure, are you using container (e.g. docker)? – Bagus Tesa Apr 14 '22 at 08:07
  • @BagusTesa Indeed it was a problem of environment of php 8.1 After reinstalling the drivers everything is ok! Thank you ! – Damien V Apr 14 '22 at 09:41

0 Answers0