How can I pass parameters to a method in my query using whereRaw?
I've got 'syntax error, unexpected '.'' when trying to add 'latitude' and 'longitude' which are column names:
public function show($lon, $lat, $radio) {
$results = Viaje::where($otherStuff)
->whereRaw( 'radio + ' . $radio . ' <= ' . $this->getDistanceFromLatLonInKm($lat, $lon, . 'latitude, longitude)');
return response()->json($results);
}
If I delete that dot, I end up passing 2 floats + 2 strings to my method instead the 4 floats required, so I get error 500.
public function show($lon, $lat, $radio)
{
$results = Viaje::where($otherStuff)->whereRaw( 'radio + ' . $radio . ' <= ' . $this->getDistanceFromLatLonInKm($lat, $lon, 'latitude', 'longitude'));
return response()->json($results);
}
Edit:
'radio', 'longitude' and 'latitude' are column names containing a float value.
public function getDistanceFromLatLonInKm($lat1,$lon1,$lat2,$lon2) {
$R = 6371; // Radius of the earth in km
$dLat = deg2rad($lat2-$lat1);
$dLon = deg2rad($lon2-$lon1);
$a = sin($dLat/2) * sin($dLat/2) + cos(deg2rad($lat1)) * cos(deg2rad($lat2))
+ sin($dLon/2) * sin($dLon/2);
$c = 2 * atan2(sqrt($a), sqrt(1-$a));
$d = R * c; // Distance in km
return $d;
}