I'm using the haversine formula to count distance from selected post-code to desired destination.I have the following scope in App\Models\Business.php:public function scopeDistance($query, $latitude, $longitude, $radius){    $query->getQuery()->orders = [];    return $query->select('*')                 ->selectRaw(( 3959 * acos( cos( radians($latitude) ) * cos( radians( latitude ) )  * cos( radians( longitude ) - radians($longitude) ) + sin( radians($latitude) ) * sin(radians(latitude)) ) ) AS distance)                 ->having('distance', '<=', $radius)                 ->orderBy('distance');}Everything seems pretty ok, except the case when I use the paginate() method instead of get() in builder.There are solutions out there, but I couldn't get these to work or were too complicated. I came up with this solution and I would like to hear feedback in terms of performance or memory usage.This is the query at the end:select count(*) as count from (select ( 3959 * acos( cos( radians(51.5872718) ) * cos( radians( latitude ) )  * cos( radians( longitude ) - radians(-0.549759) ) + sin( radians(51.5872718) ) * sin(radians(latitude)) ) ) AS distance from `businesses` having `distance` <= ?) as harversineand custom_pagination function:function custom_paginator($builder, $per_page){    $path = current_route();    $current_page = \Illuminate\Pagination\Paginator::resolveCurrentPage();    if ( ! isset($builder->getQuery()->columns[1])) $count = $builder->count();    else    {        $query = clone $builder->getQuery();        $query->columns = [ $query->columns[1] ];        $query->orders = null;        $count = array_get(\DB::select(select count(*) as count from ({$query->toSql()}) as haversine, $query->getBindings()), 0)->count;    }    return new \Illuminate\Pagination\LengthAwarePaginator(        $builder->forPage($current_page, $per_page)->get(),        $count, $per_page, null, compact('path')    );}Is there anything I should change or improve?
Laravel Pagination with haversine formula in it
php;laravel;pagination;geospatial;eloquent