Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
3
Help us understand the problem. What is going on with this article?
@koichishimizu

Eloquent でストアドプロシージャを呼ぶ

More than 5 years have passed since last update.

Eloquent でストアドプロシージャを呼ぶ

mysql> DELIMITER $$
mysql> CREATE PROCEDURE MyAwesomeProcedure(OUT output datetime)
    -> BEGIN
    ->     SELECT DATE_ADD(NOW(), INTERVAL 10 YEAR) INTO output;
    -> END;
    -> $$
Query OK, 0 rows affected (0.00 sec)

mysql> DELIMITER ;
mysql> CALL MyAwesomeProcedure(@result);
Query OK, 1 row affected (0.00 sec)

mysql> SELECT @result AS ret;
+---------------------+
| ret                 |
+---------------------+
| 2024-06-19 22:07:14 |
+---------------------+
1 row in set (0.00 sec)

mysql>
DB::select(DB::raw('CALL MyAwesomeProcedure(@result)'));
$result = DB::select('SELECT @result AS awesome');
$value = $result[0]->awesome;

2014-07-11 追記

Laravel 4.2.4 にて実行したところ、PDO 絡みの Exception が発生してしまった。

exception 'Illuminate\Database\QueryException' with message 'SQLSTATE[HY000]: General error: 2053 (SQL: CALL ...

下記のように修正したところ QueryException を回避できましたので追記してお知らせいたします。

DB::statement('CALL MyAwesomeProcedure(@result)');
$result = DB::select('SELECT @result AS awesome');
$value = $result[0]->awesome;
3
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
3
Help us understand the problem. What is going on with this article?