Php 尝试使用10公里以下的表单从数据库获取数据
我试图使用表单从数据库中获取10公里以下的经纬度记录数据,但我面临erorr erorr Illumb\Database\QueryException SQLSTATE[42S22]:未找到列: 1054“having子句”中的未知列“distance”(SQL:选择 将(*)计数为距离小于25的Php 尝试使用10公里以下的表单从数据库获取数据,php,laravel,Php,Laravel,我试图使用表单从数据库中获取10公里以下的经纬度记录数据,但我面临erorr erorr Illumb\Database\QueryException SQLSTATE[42S22]:未找到列: 1054“having子句”中的未知列“distance”(SQL:选择 将(*)计数为距离小于25的用户的集合 Web.php Route::get('/map','MapController@index'); Route::get('/userview','MapController@getUse
用户的集合
Web.php
Route::get('/map','MapController@index');
Route::get('/userview','MapController@getUserByLatLong');
Route::post('/showdata','Mapcontroller@showdata');
<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>
User.blade.php
Route::get('/map','MapController@index');
Route::get('/userview','MapController@getUserByLatLong');
Route::post('/showdata','Mapcontroller@showdata');
<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>
@csrf
纬度
经度
千分表
控制器代码
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]);
}
公共函数showdata(请求$Request)
{
$this->validate$请求[
“纬度”=>“必需”,
“经度”=>“必需”
]);
$latitude=$request->input('latitude');
$longitude=$request->input('longitude');
$users=DB::表('users')
->选择(DB::raw($latitude,$longitude,'SQRT(POW(69.1*(latitude-24.900110),2)+POW(69.1*(67.099760-经度)*COS(latitude/57.3),2))作为距离)
->哈文格罗('距离<25')
->OrderBy('距离')
->分页(10)
->get();
echo$用户;
返回视图('userview',['users'=>$users]);
}
将->havingRaw(<25')替换为havingRaw(<25'),将->havingRaw(<25')替换为havingRaw(<25'),使用whereRaw
使用如下内置方法:
$users=DB::table('users'))
->选择(‘纬度’、‘经度’、DB::raw(‘SQRT(69.1*(纬度-24.900110),2)+POW(69.1*(67.099760-经度)*COS(纬度/57.3),2))作为距离’)
->其中,原始('SQRT(功率(69.1*(?-24.900110),2)+功率(69.1*(67.099760-?)*COS(?/57.3),2))<25',数组($latitude,$longitude,$latitude))
->orderBy('距离')
->分页(10)
->get();
使用whereRaw
使用如下内置方法:
$users=DB::table('users'))
->选择(‘纬度’、‘经度’、DB::raw(‘SQRT(69.1*(纬度-24.900110),2)+POW(69.1*(67.099760-经度)*COS(纬度/57.3),2))作为距离’)
->其中,原始('SQRT(功率(69.1*(?-24.900110),2)+功率(69.1*(67.099760-?)*COS(?/57.3),2))<25',数组($latitude,$longitude,$latitude))
->orderBy('距离')
->分页(10)
->get();
我认为最好的解决方案是在用户表中添加列距离,而不是像这样在模型中添加一个变量:
public function setDistanceAttribute($value)
{
return $this->attributes['distance'] = SQRT(POW(69.1 * ($this->['latitude'] - 24.900110), 2) + POW(69.1 * (67.099760 -$this->['longitude']) * COS(latitude / 57.3), 2));
}
然后更新您的查询:
$users = DB::table('users')
->select(DB::raw($latitude,$longitude))
->havingRaw('distance < 25')
->OrderBy('distance')
->paginate(10)
->get();
$users=DB::table('users'))
->选择(数据库::原始($latitude,$longitude))
->哈文格罗('距离<25')
->OrderBy('距离')
->分页(10)
->get();
希望它能起作用:)我认为最好的解决方案是在用户表中添加列距离,而不是像这样在模型中添加一个变量:
public function setDistanceAttribute($value)
{
return $this->attributes['distance'] = SQRT(POW(69.1 * ($this->['latitude'] - 24.900110), 2) + POW(69.1 * (67.099760 -$this->['longitude']) * COS(latitude / 57.3), 2));
}
然后更新您的查询:
$users = DB::table('users')
->select(DB::raw($latitude,$longitude))
->havingRaw('distance < 25')
->OrderBy('distance')
->paginate(10)
->get();
$users=DB::table('users'))
->选择(数据库::原始($latitude,$longitude))
->哈文格罗('距离<25')
->OrderBy('距离')
->分页(10)
->get();
希望它能起作用:)解决了
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('id,name,phone,latitude,longitude, SQRT(POW(69.1 * (latitude - '.$latitude.'), 2) + POW(69.1 * ('.$longitude.'-longitude) * COS(latitude / 57.3), 2)) AS distance'))
->havingRaw('distance < 25')
->OrderBy('distance')
->get();
return view('userview',['users' => $users]);
}
公共函数showdata(请求$Request)
{
$this->validate$请求[
“纬度”=>“必需”,
“经度”=>“必需”
]);
$latitude=$request->input('latitude');
$longitude=$request->input('longitude');
$users=DB::表('users')
->选择(DB::raw('id、姓名、电话、纬度、经度、SQRT(POW(69.1*(纬度-'.$latitude'))、2)+POW(69.1*('.$latitude.'-经度)*COS(纬度/57.3)、2)作为距离)
->哈文格罗('距离<25')
->OrderBy('距离')
->get();
返回视图('userview',['users'=>$users]);
}
已解决
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('id,name,phone,latitude,longitude, SQRT(POW(69.1 * (latitude - '.$latitude.'), 2) + POW(69.1 * ('.$longitude.'-longitude) * COS(latitude / 57.3), 2)) AS distance'))
->havingRaw('distance < 25')
->OrderBy('distance')
->get();
return view('userview',['users' => $users]);
}
公共函数showdata(请求$Request)
{
$this->validate$请求[
“纬度”=>“必需”,
“经度”=>“必需”
]);
$latitude=$request->input('latitude');
$longitude=$request->input('longitude');
$users=DB::表('users')
->选择(DB::raw('id、姓名、电话、纬度、经度、SQRT(POW(69.1*(纬度-'.$latitude'))、2)+POW(69.1*('.$latitude.'-经度)*COS(纬度/57.3)、2)作为距离)
->哈文格罗('距离<25')
->OrderBy('距离')
->get();
返回视图('userview',['users'=>$users]);
}
用户表中是否有列纬度或经度?是的,用户表中有lat和long,您要选择$latitude
,$longitude
此变量?是的,我正在尝试使用lat和long获取10km以下的数据。您要选择什么值?用户表中是否有列纬度或经度?是的,我在用户表中有lat和long,您要选择$LATITUTE
,$longitude
此变量?是的,我正在尝试使用lat和long获取10km以下的数据您希望选择什么值?如果用户表中有lat和long列,则需要在查询中提及lat和long,not LATIONE和LONGTUDEI在用户表中有not lat或long其LATIONE和LONGTUDEI如果用户表中有lat和long列,则需要在查询中提到lat和long,没有纬度和经度我在用户表中没有纬度或经度,它的纬度和经度你不明白我在做什么。我试图使用10公里以下的表格获取数据。首先,当我提交表格纬度和经度时。接下来,我想在我的showdata blade页面上显示所有10公里以下的数据。@Ahteshah我已经更新。您可以将<25
更改为您想要的。先生,您不明白我在尝试做什么。我尝试使用10公里以下的表格获取数据。首先,当我提交表格纬度和经度时。接下来,我想在我的showdata blade页面上显示所有10公里以下的数据。@ahtesham我已更新。你可以换衣服