support@codebucket.net

How to read multiple query stored procedure from mssql to Laravel project

How to read multiple query stored procedure from mssql to Laravel project

K. M. Shawkat Zamil | September 29, 2022

This demonstration will show you how to get the multi-query stored procedure in the Laravel project. We have seen in a different project that we have a stored procedure that returns data in two separate parts like the below image:

To solve the problem first we have to create a route.

routes/web.php

Route::get('/get_data', [SampleController::class, 'get_data'])->name('get_data');

if we run the usp_get_data() in the Laravel project, we will get the data. For that we have to call this stored procedure like below:

public function get_data()
{
    $conn = DB::connection('sqlsrv');

    $sql = "EXEC usp_get_data";

    $pdo = $conn->getPdo()->prepare($sql);
    $pdo->execute();

    $res = array();
    do {
    array_push($res, $pdo->fetchAll());
    } while ($pdo->nextRowset());

    $data = $res[0];
    $data2 = $res[1];

    echo '<pre>'; print_r($data);
    echo '<pre>'; print_r($data2); exit();
}

if you hit the route, you will get the data as you want.

Hope this might help you in the development journey.

Read More: How to Export large data into CSV format in Laravel

K. M. Shawkat Zamil

K. M. Shawkat Zamil

Senior Software Engineer

I am a Senior Software Engineer in a reputed company in Bangladesh. I am a big fan of Laravel, PHP Programming, MSSQL Server, MySql, JavaScript, and lots more. I love to help people who are especially eager to learn. I believe in patience and motivation.