【发布时间】:2016-08-18 22:23:46
【问题描述】:
如何使用 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);
}
编辑:
“radio”、“longitude”和“latitude”是包含浮点值的列名。
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;
}
【问题讨论】:
-
getDistanceFromLatLonInKm($lat1, $lon1, $lat2, $lon2)
-
$results = Viaje::where($otherStuff)->whereRaw( 'radio + ' . $radio . ' getDistanceFromLatLonInKm($lat, $lon, '纬度', '经度'));
-
你只传递浮点数和字符串.....
$lat, $lon浮点数和`'纬度','经度'`字符串? -
我需要通过 4 个浮点数。 $lat1 和 $lon1 很简单,因为它们是我的 Show 方法接收的变量,但不确定如何传递 $lat2 和 $lon2 因为它们都是我的“纬度”和“经度”列中的值。
-
可以发一下表结构吗?