Skip to content
Advertisement

How to get records between two latitudes and longitudes using Laravel, php [closed]

enter image description here

here what I tried to get but it returns empty between 2 given coordinates

function searchListings(){
    $query = Listings::query();
    $sw_lat = request('sw_lat');
    $ne_lat = request('ne_lat');
    $sw_lng = request('sw_lng');
    $ne_lng= request('ne_lng');
    $query->whereBetween('latitude', [$sw_lat, $ne_lat])->whereBetween('longitude', [$sw_lng, $ne_lng]);
    return $query::paginate(5);
 }

Advertisement

Answer

Check datatype

Use a numeric database column type. Example of errors with a text/varchar column:

User::truncate();
User::factory()->count(1)->create(['name' => 1.5]);
User::whereBetween('name', [1,2])->paginate()->first();
// One result

User::truncate();
User::factory()->count(1)->create(['name' => -1.5]);
User::whereBetween('name', [-2,-1])->paginate()->first()
// No results (!)
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement