malhal / Laravel-Geographical

Easily add longitude and latitude columns to your records and use inherited functionality for calculating distances
MIT License
281 stars 49 forks source link

Can´t paginate while using fence #5

Open Moyhdez opened 6 years ago

Moyhdez commented 6 years ago

When i try to paginate using scopeFence i get this error:

(2/2) QueryException SQLSTATE[42S22]: Column not found: 1054 Unknown column 'distance' in 'having clause' (SQL: select count(*) as aggregate from spots inner join favorites on spots.id = favorites.spot_id where favorites.user_id = 1 having distance BETWEEN 0 AND 150)

YavorK commented 6 years ago

Yes, same with the other method, but you can do it with the query builder... DISCLAIMER: did not try it with the fence method but should be VERY similar.

public function index(Request $request)
  $requestPage = $request->input('page') ?? 1; // rewrite to $request->input('page') !== null ?  $request->input('page') : 1;  for older PHP versions
  $resultsPerPage = 30;
  $places = Place::distance($lat, $lng)
    ->orderBy('distance', 'ASC')
    ->skip(($requestPage - 1) * $resultsPerPage)
    ->take($resultsPerPage)
    ->get();
  return $places;
}
veneliniliev commented 5 years ago

check this package https://github.com/justbetter/laravel-pagination-with-havings