嗨,我一直在寻找一些如何在Laravel中指导存储过程的方法,但到目前为止还没有找到。我的问题基本上是我有一个巨大的参数列表,我需要发送到存储过程称为InsertNewApplicant
,但我不确定如何构建查询。
这是到目前为止我所拥有的,我不确定我选择将其发送到哪个数据库或如何与该数据库建立连接。
如有任何帮助,不胜感激
$result = DB::select('call InsertNewApplicant(?????????????????????????)',
array($firstName, $middleName, $addressLine_1, $addressLine_2, $postCode, $landline, $mobile, $email,
$DOB, $maritalStatus, $industry, $occupation, $jobTitle, $selfEmployed, $selfAssessment, $workHome,
$ownTransport, $companyVehicle, $paySubs, $otherIncome, $printForms, $marketingUs, $marketingOther,
$agreedTNCs, $TNCVersion, $CampaignSource));
这是我的两个数据库-我需要将这个数据发送到sqlserv数据库,所以我不知道该怎么做
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'SECRET'),
'port' => env('DB_PORT', 'SECRET'),
'database' => env('DB_DATABASE', 'SECRET'),
'username' => env('DB_USERNAME', 'SECRET'),
'password' => env('DB_PASSWORD', 'SECRET'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
'sqlsrv' => array(
'driver' => 'sqlsrv',
'host' => ' AN IP ADDRESS', // Provide IP address here
'database' => 'SECRET',
'username' => 'SECRET',
'password' => 'SECRET',
'prefix' => '',
),
您的第一个任务是配置应用程序以连接到存储过程所在的数据库。在Laravel网站上有大量的文档可以指导你如何做到这一点:https://laravel.com/docs/5.1/database
一个快速的线索是查看您的app/config/database.php
文件。查看该文件后,应该可以很明显地看到要做什么。
一旦你配置了你的数据库,确保使用DB::connection()
方法选择正确的一个。在您的示例中,您希望执行:$conn = DB::connection('sqlsrv')
以获得连接。
DB::select()
方法来调用存储过程。只要记住在每个参数占位符(问号)之间包含逗号:
$conn = DB::connection('sqlsrv');
$result = $conn->select('call InsertNewApplicant(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)',
[
$firstName, $middleName, $addressLine_1, $addressLine_2, $postCode,
$landline, $mobile, $email, $DOB, $maritalStatus, $industry,
$occupation, $jobTitle, $selfEmployed, $selfAssessment, $workHome,
$ownTransport, $companyVehicle, $paySubs, $otherIncome,
$printForms, $marketingUs, $marketingOther, $agreedTNCs,
$TNCVersion, $CampaignSource
]
);