我有以下雄辩的查询: -
$businessQuery = Business::select('businesses.id as business_id',
'users.id as user_id',\DB::raw('(
6371 *
acos(
cos( radians( '.$request->current_latitude.' ) ) *
cos( radians( address_books.latitude ) ) *
cos(
radians( address_books.longitude ) - radians( '.$request->current_longitude.' )
) +
sin(radians('.$request->current_latitude.')) *
sin(radians(address_books.latitude))
)
) as `distance`'))
->join('users', 'businesses.user_id', '=', 'users.id')
->join('address_books', 'address_books.entity_id', '=', 'businesses.id');
$businessQuery = $businessQuery->where(array(
'businesses.is_deleted'=>'N',
'users.is_blocked'=>'N',
'users.is_active'=>1,
'address_books.entity_type'=>'M',
));
if($request->business_name != '')
{
$businessQuery = $businessQuery->where('businesses.business_name', 'like', '%' . $request->business_name . '%');
}
if($request->owner_name != '')
{
$businessQuery = $businessQuery->where('businesses.owner_name', 'like', '%' . $request->owner_name . '%');
}
if($request->service_men != '')
{
$businessQuery = $businessQuery->where('businesses.service_men', 'like', '%' . $request->service_men . '%');
}
if($request->service_women != '')
{
$businessQuery = $businessQuery->where('businesses.service_women', 'like', '%' . $request->service_women . '%');
}
if($request->service_kids != '')
{
$businessQuery = $businessQuery->where('businesses.service_kids', 'like', '%' . $request->service_kids . '%');
}
if($request->distance != '')
{
$businessQuery = $businessQuery->havingRaw('distance < '.$request->distance);
}
现在,如果我想获得上述Eloquent的计数,我会收到错误:
$totalCount = $businessQuery->count();
但是当我在$ businessQuery上运行get()时,我没有问题。
$businessResult = $businessQuery->get();
我做错了什么?
这是我得到的错误: -
Column not found: 1054 Unknown column 'distance' in '
having clause' (SQL: select count(*) as aggregate from `businesses`
inner join `users` on `businesses`.`user_id` = `users`.`id` inner join
`address_books` on `address_books`.`entity_id` = `businesses`.`id`
where (`businesses`.`is_deleted` = N and `users`.`is_blocked` = N and `users`.`is_active` = 1 and `address_books`.`entity_type` = M)
having distance < 200)