获取存储过程Laravel的结果

时间:2017-03-11 16:08:19

标签: php mysql laravel stored-procedures laravel-5

我使用的是Laravel 5.4和MySql 5.7,PHP 5.6。 我试图调用一个存储过程(执行SELECT查询,连接多个表),并将结果返回给我的PHP。

以下是我尝试失败的事情:

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);

结果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);

结果2:

array(0) {
}

**存储过程:**

    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 ;

注意:

我当然已经检查过了,当我这样调用它时,我的查询得到结果:

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);

有没有人已经遇到过这个问题? 感谢您的阅读

3 个答案:

答案 0 :(得分:1)

您应该将CALL your_procedure通过DB::raw传递给DB::SELECT

所以这将起作用并返回结果集:

$p0 = Carbon::now();
$p1 = Carbon::now()->addDays(7);
$p2 = 100;
$p3 = 2;
DB::select(DB::raw("CALL rentalsAvailables_get($p0, $p1, $p2, $p3)"));

您也可以使用hydrate方法将结果集转换为 Eloquent model

Foo::hydrate($result_from_db_select);

希望这可以帮助你6个月后:))

答案 1 :(得分:0)

你看到这两个值

2017-03-11 16:00:42, 2017-03-18 16:00:42//they are not escaped that's your problem

解决方案 -

$result =  \DB::statement('CALL rentalsAvailables_get(\'' . Carbon::now() . '\', \'' . Carbon::now()->addDays(7) . '\', 100, 2)')->fetchAll();

答案 2 :(得分:0)

$result =  DB::select('CALL rentalsAvailables_get(?, ?, ?, ?)',
            array(
                'p0' => Carbon::now(),
                'p1' => Carbon::now()->addDays(7),
                'p2' => 100,
                'p3' => 2
            )
);
上面的

是通过索引绑定param =&GT;只需要下面就足够了

$result =  DB::select('CALL rentalsAvailables_get(?, ?, ?, ?)',
            array(
                Carbon::now(),
                Carbon::now()->addDays(7),
                100,
                2
            )
);

请阅读有关PHP PDO的更多信息