Laravel call Database Stored Procedure with Eloquent ORM, Query Builder | MySQL


How to call MySQL Stored Procedure with Laravel Eloquent ORM, Query Builder

At the end of the last summer, I received the following email in my inbox, from one of my follower on techalyst.com, seeking my help whether it is possible to call Database Stored Procedure with Laravel Eloquent, 

Event though it is not possible to execute MySQL Stored procedure with Laravel Eloquent ORM directly , you can use Laravel Query builder approach in the following style.

first import the following namespaces on top of the controller file or in the file where you want to call the Stored Procedure.

use Illuminate\Support\Facades\DB;
use Doctrine\DBAL\Driver\PDOConnection;

and following lines of code show how to call the stored procedure, you can bind parameters required by your stored procedure.

$db = DB::connection()->getPdo();
$db->setAttribute(PDOConnection::ATTR_ERRMODE, PDOConnection::ERRMODE_EXCEPTION);
$db->setAttribute(PDOConnection::ATTR_EMULATE_PREPARES, true);

$queryResult = $db->prepare('call yourstoredprocedurename(?,?)'); 
$queryResult->bindParam(1, $parameter1); 
$queryResult->bindParam(2, $parameter2,PDOConnection::PARAM_STR);  
$queryResult->execute(); 
$results = $queryResult->fetchAll(PDOConnection::FETCH_ASSOC); 
$queryResult->closeCursor(); 
return $results;


if you have any questions regarding "how to call MySQL Stored Procedure with Laravel Eloquent ORM, Query Builder", please feel free to leave your comment bellow.


Written by Akram Wahid 5 years ago

are you looking for a chief cook who can well craft laravel and vuejs, to make some awsome butterscotch,
yes then it is right time for you to look at my profile.

Do you want to write Response or Comment?

You must be a member of techalyst to proceed!

Continue with your Email ? Sign up / log in

Responses

Be the first one to write a response :(

{{ item.member.name }} - {{ item.created_at_human_readable }}

{{ reply.member.name }} - {{ reply.created_at_human_readable }}