使用分页连接中的where子句时出现奇怪的行为

时间:2015-01-09 17:06:56

标签: php laravel-4 pagination

我有以下代码:

$products = DB::table('products AS p')
                ->select('p.*', 'pi.image', 'b.name AS brand')
                ->leftJoin('products_images AS pi', function($join)
                {
                    $join->on('p.id', '=', 'pi.id_product')
                        ->where('pi.primary', '=', 2);
                })
                ->leftJoin('brands AS b', 'p.id_brand', '=', 'b.id')
                ->where('p.is_active', 1)
                ->where('id_category', 31)
                ->orderBy('p.name')
                ->get();

它完美无缺,但是当我用paginate()

替换get()时
$products = DB::table('products AS p')
                ->select('p.*', 'pi.image', 'b.name AS brand')
                ->leftJoin('products_images AS pi', function($join)
                {
                    $join->on('p.id', '=', 'pi.id_product')
                        ->where('pi.primary', '=', 2);
                })
                ->leftJoin('brands AS b', 'p.id_brand', '=', 'b.id')
                ->where('p.is_active', 1)
                ->where('id_category', 31)
                ->orderBy('p.name')
                ->paginate(15);

它错误地生成了一个查询(所有'其中'条件是混合的):

select `p`.*, `pi`.`image`, `b`.`name` as `brand` from `products` as `p` 
left join `products_images` as `pi` on `p`.`id` = `pi`.`id_product` and `pi`.`primary` = '2'
left join `brands` as `b` on `p`.`id_brand` = `b`.`id` 

where `p`.`is_active` = '2' and `id_category` = '1' 

order by `p`.`name` asc limit 15 offset 0

当我删除'其中'匿名函数中的子句,一切正常。 这是Laravel的一个错误吗?或者我做错了什么?

0 个答案:

没有答案