如何将参数传递给使用whereRaw的查询中的方法?将参数传递给使用whereRaw的查询中的方法
我有“语法错误,意想不到的”“”尝试添加时,“纬度”和“经度”,这是列名:
public function show($lon, $lat, $radio) {
$results = Viaje::where($otherStuff)
->whereRaw('radio + ' . $radio . ' <= ' . $this->getDistanceFromLatLonInKm($lat, $lon, . 'latitude, longitude)');
return response()->json($results);
}
如果我删除点,我最终通过2个漂浮+ 2串到我的方法,而不是所需要的4个浮点,所以我得到错误500
public function show($lon, $lat, $radio)
{
$results = Viaje::where($otherStuff)->whereRaw('radio + ' . $radio . ' <= ' . $this->getDistanceFromLatLonInKm($lat, $lon, 'latitude', 'longitude'));
return response()->json($results);
}
编辑:
“无线电”,“经度”和“纬度”是含有一个浮子列名值。
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;
}
哪种方法? – jaysingkar
getDistanceFromLatLonInKm($ lat1,$ lon1,$ lat2,$ lon2) – lulu666
$ results = Viaje :: where($ otherStuff) - > whereRaw('radio +'。$ radio。'<='。$ this-> getDistanceFromLatLonInKm( $ lat,$ lon,'latitude','longitude')); – jaysingkar