简体   繁体   中英

How to create stored procedure laravel

Hi I've been looking around for some kind of how to guide for stored procedures in Laravel but can't find any so far. My issue is basically I have a huge list of parameters that I need to send to the stored procedure called InsertNewApplicant but I'm not sure how to build the query.

This is all I have so far, I'm not sure where I chose which database to send it to or how to make the connection to that database.

Any help would be greatly appreciated

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

Here's my two databases - I need to send this data to the sqlserv database so I'm not sure what to do

'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' => '',
),

Your first task is to configure your application to connect to the database that your stored procedure is in. There is plenty of documentation on the Laravel website that will guide you towards how to do this: https://laravel.com/docs/5.1/database

A quick clue is to look inside your app/config/database.php file. It should be fairly obvious what to do once you look at that file.

Once you've configured your database, make sure to select the correct one using the DB::connection() method. In your case, you would want to do: $conn = DB::connection('sqlsrv') to get the connection.

Once you have added your database connection configuration, you should call your stored procedure by calling the DB::select() method. Just remember to include commas between each of the parameter placeholders (question marks):

$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
  ]
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM