我使用laravel v5已经有一段时间了,连接到一个homestead/vagrant虚拟机并集成到本地mysql数据库中。但是,我现在需要通过同一个虚拟机远程连接到我网络上的mssql数据库。
最初我收到
driver not found
尝试使用时出错
sqlsrv
连接,但我通过安装
php7.1-sybase
包裹。我现在可以确认
php -m
表明
pdo_dblib
已安装并启用。
我已经在我的
.env
要使用第二个数据库连接的文件:
DB_CONNECTION_SECOND=sqlsrv
DB_HOST_SECOND=<server>
DB_PORT_SECOND=1433
DB_DATABASE_SECOND=<database>
DB_USERNAME_SECOND=<username>
DB_PASSWORD_SECOND=<password>
我可以成功地从我的虚拟机ping服务器。
在我的模型中,我指定了
$connection
和
$table
参数:
/**
* The database name used by the model.
*
* @var string
*/
protected $connection = 'sqlsrv';
/**
* The database table used by the model.
*
* @var string
*/
protected $table = '<tablename>';
在我的控制器中,我只是试图使用where claus返回一条记录:
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$employees = Employee::where( 'LastName', '=', 'Bloggs' )->get();
return $employees;
}
但是,我尝试建立的任何连接都会导致
502 Bad Gateway
错误。我也试过连接到网络上的不同数据库,但仍然得到相同的错误。