mysql存储过程laravel,获取存储过程Laravel的结果

I am using Laravel 5.4 and MySql 5.7, PHP 5.6.

I am trying to call a stored procedure (that does a SELECT query, joining multiple tables), and to get the result returned to my PHP.

Here are the things I've unsuccessfully tried:

PHP 1:

$result = DB::select('CALL rentalsAvailables_get(?, ?, ?, ?)',

array(

'p0' => Carbon::now(),

'p1' => Carbon::now()->addDays(7),

'p2' => 100,

'p3' => 2

)

);

return var_dump($result);

Result 1:

{

"error": {

"message": "SQLSTATE[HY000]: General error: 2031 (SQL: CALL rentalsAvailables_get(2017-03-11 16:00:42, 2017-03-18 16:00:42, 100, 2))",

"code": "HY000",

"status_code": 500

}

}

PHP 2:

$result = DB::select('CALL rentalsAvailables_get(:p0, :p1, :p2, :p3)',

array(

'p0' => Carbon::now(),

'p1' => Carbon::now()->addDays(7),

'p2' => 100,

'p3' => 2

)

);

return var_dump($result);

Result 2:

array(0) {

}

** Stored Procedure: **

DELIMITER $$

CREATE DEFINER=`root`@`localhost` PROCEDURE `rentalsAvailables_get`(IN `startDate` DATETIME, IN `endDate` DATETIME, IN `maxPrice` INT(11) ZEROFILL, IN `capacity` INT(2))

NO SQL

SELECT a.rentalId, a.rentalName, a.rentalBathroom, a.rentalBedroom, a.rentalCapacity, a.rentalCover, a.rentalLat, a.rentalLong, a.rentalLocation, a.rentalStatus, a.rentalSummary, a.rentalText, a.rentalOwnerId,

b.rentalTypeId, b.rentalTypeName,

c.id as 'userId', c.name,

d.rentalPriceId, d.rentalPrice

FROM rentals as a

INNER JOIN rentalTypes as b

ON a.rentalTypeId = b.rentalTypeId

INNER JOIN users as c

ON a.rentalOwnerId = c.id

INNER JOIN rentalprice as d

ON a.rentalId = d.rentalId

WHERE a.rentalCapacity > @p3

AND a.rentalStatus = true

AND d.rentalPrice < @p2

AND d.rentalPriceStartDate <= @p0

AND d.rentalPriceEndDate >= @p1

AND a.rentalId NOT IN (

SELECT eSub.rentalId FROM unavailabilities as eSub WHERE eSub.unavailabilityStartDate >= @p0 AND eSub.unavailabilityEndDate <= @p1

)

AND a.rentalId NOT IN (

SELECT fSub.rentalId FROM rent as fSub WHERE fSub.rentStartDate >= @p0 AND fSub.rentEndDate <= @p1

)$$

DELIMITER ;

Note:

I have of course checked and i am getting results from my query when calling it like that:

SET @p0='2017-03-11 04:26:09.000000';

SET @p1='2017-03-17 04:26:09.000000';

SET @p2='1000';

SET @p3='2';

CALL `rentalsAvailables_get`(@p0, @p1, @p2, @p3);

Did anyone already got this issue?

Thanks for reading

解决方案

You should pass CALL your_procedure via DB::raw to DB::SELECT.

So this will work and it returns result set:

$p0 = Carbon::now();

$p1 = Carbon::now()->addDays(7);

$p2 = 100;

$p3 = 2;

DB::select(DB::raw("CALL rentalsAvailables_get($p0, $p1, $p2, $p3)"));

Also you can convert the result set to Eloquent model using hydrate method.

Foo::hydrate($result_from_db_select);

Hope this help you after 6 months :))

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Laravel 中创建视图非常简单,只需要在 `resources/views` 目录下创建一个 `.blade.php` 后缀的文件即可。视图中可以包含 HTML、CSS、JavaScript 等前端代码,并且可以通过 Blade 模板引擎来方便地嵌入 PHP 代码。 在视图中,我们可以通过控制器向视图中传递数据。一般来说,我们可以通过以下两种方式来传递数据: 1. 使用 `with` 方法传递数据: 在控制器中,可以使用 `with` 方法来传递数据。例如: ```php public function index() { $data = ['name' => 'John', 'age' => 30]; return view('welcome')->with($data); } ``` 在视图中,可以使用以下方式来获取传递过来的数据: ```php <h1>Welcome {{ $name }}</h1> <p>Your age is {{ $age }}</p> ``` 2. 使用数组传递数据: 在控制器中,可以将数据存储在一个数组中,并将该数组传递到视图中。例如: ```php public function index() { $data = ['name' => 'John', 'age' => 30]; return view('welcome', $data); } ``` 在视图中,可以使用以下方式来获取传递过来的数据: ```php <h1>Welcome {{ $name }}</h1> <p>Your age is {{ $age }}</p> ``` 除了以上两种方式,我们还可以使用 `compact` 方法来传递数据。例如: ```php public function index() { $name = 'John'; $age = 30; return view('welcome', compact('name', 'age')); } ``` 在视图中,可以使用以下方式来获取传递过来的数据: ```php <h1>Welcome {{ $name }}</h1> <p>Your age is {{ $age }}</p> ``` 值得注意的是,如果我们需要在多个控制器方法中传递相同的数据,可以使用视图的共享数据功能。具体做法是,在 `AppServiceProvider` 类的 `boot` 方法中使用 `view` 函数来共享数据。例如: ```php public function boot() { view()->share('siteName', 'My Blog'); } ``` 在视图中,可以使用以下方式来获取共享的数据: ```php <h1>Welcome to {{ $siteName }}</h1> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值