【发布时间】:2020-05-11 23:15:51
【问题描述】:
我正在尝试使用表单通过纬度和经度从数据库中获取 10 公里以下记录的数据,但我面临错误
错误
Illuminate\Database\QueryException SQLSTATE[42S22]:找不到列: 1054“有子句”中的未知列“距离”(SQL:选择 count(*) 作为来自
users的聚合,距离
Web.php
Route::get('/map','MapController@index');
Route::get('/userview','MapController@getUserByLatLong');
Route::post('/showdata','Mapcontroller@showdata');
User.blade.php
<form method="POST" action="{{ action('Mapcontroller@showdata') }}">
@csrf
<lable>Latitude</lable>
<input type="text" name="latitude" /><br>
<lable>Longitude</lable>
<input type="text" name="longitude" /><br>
<lable>Kilomneter</lable>
<input type="text" name="kilometer" /><br>
<input type="submit" name="submit" /><br>
</form>
控制器代码
public function showdata(Request $request)
{
$this->validate($request, [
'latitude' => 'required',
'longitude' => 'required'
]);
$latitude = $request->input('latitude');
$longitude = $request->input('longitude');
$users = DB::table('users')
->select(DB::raw($latitude,$longitude, 'SQRT(POW(69.1 * (latitude - 24.900110), 2) + POW(69.1 * (67.099760 -longitude) * COS(latitude / 57.3), 2)) AS distance'))
->havingRaw('distance < 25')
->OrderBy('distance')
->paginate(10)
->get();
echo $users;
return view('userview',['users' => $users]);
}
【问题讨论】:
-
用户表中有纬度或经度列吗?
-
是的,我在用户表中有纬度和经度
-
你想选择
$latitude,$longitude这个变量吗? -
是的,我正在尝试获取 10 公里以下的经纬度数据
-
你要选择什么值?