How to Execute Stored Procedure from Laravel

How to execute Stored Procedure from Laravel

Try something like this

DB::select('exec my_stored_procedure("Param1", "param2",..)');

or

DB::select('exec my_stored_procedure(?,?,..)',array($Param1,$param2));

Try this for without parameters

DB::select('EXEC my_stored_procedure')

How to execute Stored Procedure from Laravel - SQL Server

It's returning everything as you are using select * which tells the database that you want the entire row. If you wish to only get a specific column then you need to change that to select id_verifikator.

A bit off-topic but I would suggest that you just run this query in Laravel instead of having a procedure, especially as this is such a basic query. The below links can help you get started.

https://laravel.com/docs/8.x/queries

https://laravel.com/docs/8.x/eloquent

Executing stored procedure in php laravel

Always try to use parameters in your statements to prevent possible SQL injection issues. As an additional note, use unambiguous date format, when you pass date values to SQL Server:

Example using PHP Driver for SQL Server:

<?php
// Connection
$serverName = env("DB_HOST");
$connectionInfo = array(
"Database"=>env("DB_DATABASE"),
"UID"=>env("DB_USERNAME"),
"PWD"=>env("DB_PASSWORD")
);
$conn = sqlsrv_connect($serverName, $connectionInfo);
if ($conn === false) {
die(print_r(sqlsrv_errors(), true));
}

// Statement
$employee = '000010993';
$datefrom = '20200601';
$dateto = '20200610';
$tsql = "EXEC USP_Daily_TA_Punching_Detailswith_OT ?, ?, ?";
$params = array($employee, $datefrom, $dateto);
$getResults = sqlsrv_query($conn, $tsql, $params);
if ($getResults === false) {
die(print_r(sqlsrv_errors(), true));
}

// Results
$data = array();
do {
while ($row = sqlsrv_fetch_array($getResults, SQLSRV_FETCH_ASSOC)) {
$data[] = $row;
}
} while (sqlsrv_next_result($getResults));

// End
sqlsrv_free_stmt($getResults);
sqlsrv_close($conn);
$total_row = count($data);
?>

Example using Laravel:

<?php

...
$employee = '000010993';
$datefrom = '20200601';
$dateto = '20200610';
DB::select("SET NOCOUNT ON; EXEC USP_Daily_TA_Punching_Detailswith_OT ?, ?, ?", array($employee, $datefrom, $dateto));

...
?>

How to call stored procedure on Laravel?

You change.

return DB::select('call sp_clientupdate("108", "Sandeep","0999999999","","","sandeep@gmail.com","","","","","","",)');

Try it's working perfectly....



Related Topics



Leave a reply



Submit