I'm currently on a fresh install of Laravel 6 and trying to connect it to my Microsoft SQL Server, and I am serving it locally using Laragon.
I have downloaded the ODBC driver for SQL Server.
My database.php file contains:
'default' => env('DB_CONNECTION', 'sqlsrv'),
...
'sqlsrv' => [
'driver' => 'sqlsrv',
'url' => env('DATABASE_URL'),
'odbc' => true, // I added this line
'odbc_datasource_name' => '{SQL Server}', // I added this line
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '1433'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
'prefix_indexes' => true,
],
And my .env contains the valid login credentials. To test that I can connect to the DB, I did a check in welcome.blade.php:
@php
if (DB::connection()->getDatabaseName())
{
echo "connected successfully to database ".DB::connection()->getDatabaseName();
}
@endphp
Which echos onto my page as:
connected successfully to database testDatabase
I have gone into Tinker to see if I can get a Collection of all Employees from my model, i.e. Carer.php:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Carer extends Model
{
protected $table = "dbo.carer";
protected $primaryKey = 'CarerID';
}
...
$carer = App\Carer::all();
Which returns:
Illuminate/Database/QueryException with message 'could not find driver (SQL: select * from [dbo].[carer])'
Does this mean my check for connecting to the database is faulty if I cannot find a driver when attempting to retrieve model data? Do I have to do something specific in Laragon to get it to work for SQL Server?
Thank you.
UPDATE: Following the instructions from here and here, I have managed to add the PDO drivers to my php.ini file and get it running with Laragon. When I print out phpinfo()
I can now see the sql servers.
The error I am now getting is:
Illuminate/Database/QueryException with message 'SQLSTATE[08001]: [Microsoft][ODBC Driver 17 for SQL Server]TCP Provider: No connection could be made because the target machine actively refused it. (SQL: select top 1 * from [dbo].[carer])'
Answering this in case anybody else gets stuck on the same point.
My Microsoft SQL Server didn't have connection over TCP/IP enabled.
You want to:
Open SQL Server Configuration Manager
Under SQL Server Network Configuration, select Protocols for MSSQLSERVER
Right click on TCP/IP and click Enable
Restart SQL Server (MSSQLSERVER)