Laravel 5.3无法使用派生字段对模型进行分页

时间:2016-09-19 21:23:58

标签: php laravel laravel-5 eloquent laravel-5.3

我有一个Property模型,我已经做了以下范围:

public function scopeSearch($query, $search)
{
    return $this->addSelect(DB::raw("( 3959 * acos( cos( radians(".$search['latitude'].") ) * cos( radians( properties.latitude ) )
                      * cos( radians( properties.longitude ) - radians(".$search['longitude'].") ) + sin( radians(".$search['latitude'].") )
                      * sin( radians( properties.latitude ) ) ) ) AS distance"))
        ->having('distance', '<=', $search['radius']);
}

如果我然后尝试获得一些分页结果:

$properties = Property::search($search)
        ->addSelect('properties.*')
        ->paginate($this->per_page);

我收到以下错误:

QueryException in Connection.php line 729:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'distance' in 'having clause' (SQL: select count(*) as aggregate from `properties` where `properties`.`deleted_at` is null having `distance` <= 25)

看起来分页正在执行不运行addSelect()方法的计数查询,因此字段距离不存在。有没有办法让它添加选择?

完整堆栈跟踪:

in Connection.php line 729
at Connection->runQueryCallback('select count(*) as aggregate from `properties` where `properties`.`deleted_at` is null having `distance` <= ?', array('25'), object(Closure)) in Connection.php line 685
at Connection->run('select count(*) as aggregate from `properties` where `properties`.`deleted_at` is null having `distance` <= ?', array('25'), object(Closure)) in Connection.php line 349
at Connection->select('select count(*) as aggregate from `properties` where `properties`.`deleted_at` is null having `distance` <= ?', array('25'), true) in Builder.php line 1610
at Builder->runSelect() in Builder.php line 1596
at Builder->get() in Builder.php line 1671
at Builder->getCountForPagination() in Builder.php line 491
at Builder->paginate('10') in PropertiesController.php line 143
at PropertiesController->search_results(object(Request))
at call_user_func_array(array(object(PropertiesController), 'search_results'), array(object(Request))) in Controller.php line 80
at Controller->callAction('search_results', array(object(Request))) in ControllerDispatcher.php line 146
at ControllerDispatcher->call(object(PropertiesController), object(Route), 'search_results') in ControllerDispatcher.php line 94
at ControllerDispatcher->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
at Pipeline->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in ControllerDispatcher.php line 96
at ControllerDispatcher->callWithinStack(object(PropertiesController), object(Route), object(Request), 'search_results') in ControllerDispatcher.php line 54
at ControllerDispatcher->dispatch(object(Route), object(Request), 'App\Http\Controllers\PropertiesController', 'search_results') in Route.php line 174
at Route->runController(object(Request)) in Route.php line 140
at Route->run(object(Request)) in Router.php line 724
at Router->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in VerifyCsrfToken.php line 64
at VerifyCsrfToken->handle(object(Request), object(Closure))
at call_user_func_array(array(object(VerifyCsrfToken), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in ShareErrorsFromSession.php line 49
at ShareErrorsFromSession->handle(object(Request), object(Closure))
at call_user_func_array(array(object(ShareErrorsFromSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in StartSession.php line 64
at StartSession->handle(object(Request), object(Closure))
at call_user_func_array(array(object(StartSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 37
at AddQueuedCookiesToResponse->handle(object(Request), object(Closure))
at call_user_func_array(array(object(AddQueuedCookiesToResponse), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in EncryptCookies.php line 59
at EncryptCookies->handle(object(Request), object(Closure))
at call_user_func_array(array(object(EncryptCookies), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in Router.php line 726
at Router->runRouteWithinStack(object(Route), object(Request)) in Router.php line 699
at Router->dispatchToRoute(object(Request)) in Router.php line 675
at Router->dispatch(object(Request)) in Kernel.php line 246
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in CheckForMaintenanceMode.php line 44
at CheckForMaintenanceMode->handle(object(Request), object(Closure))
at call_user_func_array(array(object(CheckForMaintenanceMode), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in Kernel.php line 132
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 99
at Kernel->handle(object(Request)) in index.php line 59

1 个答案:

答案 0 :(得分:2)

您可以做的是将distance列的整个定义复制到having部分。实际上,您应该使用havingRaw函数来实现此目的。

但是,addSelecthavingRaw都应该使用绑定,因为现在您的代码很容易受到SQL注入的攻击。<​​/ p>