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();