how to fetch multiple result set from a mysql stored procedure in laravel

前端 未结 2 423
梦毁少年i
梦毁少年i 2020-12-18 13:21

I want to fetch multiple result sets from a stored procedure in laravel. Is there a way I can do this? Currently, I can get a single row\'s data using the below code:

<
2条回答
  •  孤城傲影
    2020-12-18 13:30

    If your stored procedure returns multiple outputs, In this case, you can handle this situation by two methods.

    1. Goto vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php and set PDO::ATTR_EMULATE_PREPARES as true.

    It will create problems when you will developing APIs, because of all APIs return numbers as a string when response return in JSON Like: {"status": "1"} but it should be {"status": 1}

    1. Add a new connection to a particular situation. You have to handle the above discussed problem for a particular API only not in all APIs. So I suggest choosing option 2nd.

    Add connection in the config/database.php use below code into connections

    'mysql_procedure' => [
    'driver' => 'mysql',
    'host' => env('DB_HOST', '127.0.0.1'),
    'port' => env('DB_PORT', '3306'),
    'database' => env('DB_DATABASE', 'forge'),
    'username' => env('DB_USERNAME', 'forge'),
    'password' => env('DB_PASSWORD', ''),
    'unix_socket' => env('DB_SOCKET', ''),
    'charset' => 'utf8mb4',
    'collation' => 'utf8mb4_unicode_ci',
    'prefix' => 'sv_',
    'prefix_indexes' => true,
    'strict' => false,
    'engine' => null,
    'options' => extension_loaded('pdo_mysql') ? array_filter([
    PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),PDO::ATTR_EMULATE_PREPARES => true
    ]) : [],
    ],
    

    update details as per your configuration.

    $procRslts = DB::connection('mysql_procedure')
    ->select("CALL user_login(?,?,?,?)", array( $userId ,$password,$success,$firstName ));
    

    I think in this you don't need to pass the last two parameters, you can write logic int procedure to get from the database.

    You can get multiple outputs returned by the stored procedure.

提交回复
热议问题