3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

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

Last updated at Posted at 2014-06-19

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
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?