Laravel 4.2 - 使用Eloquent paginate函数时左连接isseu

时间:2015-09-02 04:58:53

标签: php laravel laravel-4 pagination

我在执行以下查询时遇到问题:

$businesses = Business::leftJoin('business_stats','businesses.id','=','business_stats.business_id')
    ->leftJoin('restaurant_coupons',
        function($join){
            $currentdate    =   date("Y-m-d");
            $join->on('restaurant_coupons.business_id','=','businesses.id')
                ->whereNull('restaurant_coupons.deleted_at')
                ->where('restaurant_coupons.starts','<=',$currentdate)
                ->where('restaurant_coupons.ends','>=',$currentdate);
        }
    )
   ->where('is',1)
    ->select('businesses.*'
                ,'business_stats.*'
                ,DB::raw( $nullQuery )
                ,'businesses.id as id','restaurant_coupons.code')  
    ->orderBy($orderBy,$orderByOrder)
    ->paginate(30);

以下是Laravel准备好的声明的查询日志:

Array
(
[query] =>
            select
                `businesses`.*,
                `business_stats`.*,
                IFNULL(tit_rank,100000) as titrank,
                `businesses`.`id` as `id`,
                `restaurant_coupons`.`code`
            from
                `businesses`
            left join `business_stats` on
                    `businesses`.`id` = `business_stats`.`business_id`
            left join `restaurant_coupons` on
                `restaurant_coupons`.`business_id` = `businesses`.`id` and
                `restaurant_coupons`.`deleted_at` is null and
                `restaurant_coupons`.`starts` <= ? and
                `restaurant_coupons`.`ends` >= ?
            where
                `businesses`.`deleted_at` is null and
                `is_bolo` = ?
                order by
                    `titrank` desc
                limit 30 offset 0
[bindings] => Array
    (
        [0] => 2015-09-01
        [1] => 2015-09-01
        [2] => 2015-09-01
        [3] => 2015-09-01
        [4] => 1
    )

[time] => 0.55
)

执行查询后,我得到null作为输出。但它应该返回结果。

经过一些调试后,我发现如果删除->paginate(30)并添加->get(),则会按预期获得结果。

我开始知道paginate函数在查询中将数据绑定两次。这是我的查询只期望3个参数,但在paginate函数中,查询被调用两次,第二次传递另外两个参数,即5个参数。

如果您看到参数数组有5个元素,其中第一个和第二个元素在第三个和第四个位置再次重复。

0 个答案:

没有答案