I need to fetch the data from table "properties" where I am storing the address of the property in longitude and latitude and other fields like construction_type,no_of_bedrooms,no_of_bathrooms. Now I need to filter the data according to the nearest location given with passing other filters like construction_type,no_of_bedrooms,no_of_bathrooms.
I am using Haversine formula to fetch the nearest locations by given location. I am having complexities in writing the laravel eloqent query when passing other filters as well.
$property = (new Property())->newQuery();
if(\Request::get('Lat')!=null && \Request::get('Lng')!=null){
$lat=\Request::get('Lat');
$lng=\Request::get('Lng');
$radius=200;
$q="( 3959 * acos( cos( radians(' . $lat . ') ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(' . $lng . ') ) + sin( radians(' . $lat .') ) * sin( radians(lat) ) ) )";
$property->selectRaw("{$q} AS distance")->havingRaw("distance < ?", [$radius]);
}
if(\Request::get('construction_status')!=null){
$property->where('construction_status', \Request::get('construction_status'));
}
//other filters
return $property->get();
I expect the result as the properties with the nearest location given and other filters as well
select()
?$property = (new Property())->newQuery();