我正在按距离和搜索词进行搜索查询,但是当使用“有”在雄辩时,标准的分页不再有效。
这是我的代码:
$haversine = '( 3959 * acos( cos( radians(?) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians(?) ) + sin( radians(?) ) * sin( radians( latitude ) ) ) )';
$stores = Store::select(
DB::raw("*, $haversine AS distance"))
->having("distance", "<", $input['distance'])
->orderBy("distance")
->setBindings(array(
$latitude,
$longitude,
$latitude
))
->with(array(
'offers' => function($query) {
if(Input::has('term')) {
$query->whereHas('product', function ($product_query) {
$product_query->where(function($term_query) {
$fields = array('name', 'description','model_number');
foreach($fields as $field) {
$term_query->orWhere($field, 'LIKE', '%' . Input::get('term') . '%');
}
$term_query->orWhereHas('brand', function($brand_query) {
$brand_query->where('name', 'LIKE', '%' . Input::get('term') . '%');
});
});
});
}
$query->orderBy('price', 'ASC');
})
)
->get();这个查询在没有分页的情况下工作得很好,但是当尝试标准->paginat(10)时,我得到了以下内容:
SQLSTATE42S22:未找到列:在“from子句”中有1054个未知列“距离”(SQL:从具有
distance<26.1817的stores中选择count(*)作为聚合项)
我已经做了大量的寻找这个答案,但我不知道为什么我找到的解决方案不适用于我。
我已经看过了:https://github.com/laravel/framework/issues/3105
和
How to use paginate() with a having() clause when column does not exist in table
如果你过去处理过这个问题,请给我一些指导。
编辑:
下面是我尝试过的代码,但没有对我正常工作。
$current_page = Paginator::getCurrentPage();
$paginated_query = clone $stores;
$paginated_query->addSelect('stores.*');
$items = $paginated_query->forPage($current_page, 10)->get();
$totalResult = $stores->addSelect(DB::raw('count(*) as count'))->get();
$totalItems = $totalResult[0]->count;
$stores = Paginator::make($items->all(), $totalItems, 10);发布于 2015-11-10 22:06:51
我也有同样的问题,在分页使用‘有’子句在雄辩。
这对我起了作用:
使用实际的计算,而不是在“having”子句中使用列的别名。
而不是:
$model->select(DB::raw("*, $haversine AS distance"))->having("distance", "<", $input['distance']);试试这个:
$model->select("*")->having(DB:raw($haversine), "<", $input['distance']);https://stackoverflow.com/questions/28905760
复制相似问题