问题
There is no standard procedure mentioned in documentation that how to call a stored procedure in MYSQL in laravel with Eloquent syntax.
I mean is there any way to call stored procedure in mysql with help of eloquent syntax like
$result = StoredProcedures::my_custom_procedure(param1, param2);
return view('welcome')->with('result',$result);
Is there any way to call and fetch results from stored procedures with pure eloquent syntax(purely eloquent way).
Thanks. Small examples are highly encouraged .
回答1:
You cannot call stored procedure with eloquent. However you can use 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;
then you can call the stored procedure like below,
$queryResult = $db->prepare('call searchAttendance(?,?,?,?,?)');
$queryResult->bindParam(1, $start_date);
$queryResult->bindParam(2, $end_date);
$queryResult->bindParam(3, $nurseRoles,PDOConnection::PARAM_STR);
$queryResult->bindParam(4, $leaveTypesDayOffIds,PDOConnection::PARAM_STR);
$queryResult->bindParam(5, $leaveTypesVactionIds,PDOConnection::PARAM_STR);
$queryResult->execute();
$results = $queryResult->fetchAll(PDOConnection::FETCH_ASSOC);
$queryResult->closeCursor();
return $results;
reference: Click here
回答2:
As everyone has mentioned, you can't call a stored procedure in Laravel with Eloquent syntax. You can transfer the result of your query to a collection which will give you all of the collection functionality.
$queryResult = DB::select('call my_custom_procedure(?, ?)', [1,2]);
$result = collect($queryResult);
return view('welcome')->with('result',$result);
回答3:
No there is no Facade
for that a simple solution is to use a raw SQL
query
$result = DB::select('my_custom_procedure(?, ?)', [1,2]);
来源:https://stackoverflow.com/questions/44354611/how-to-call-stored-procedure-with-eloquent-laravel