4 回答
TA贡献1864条经验 获得超6个赞
我认为最适合您的解决方案是在用户表中添加列距离,而不是在模型中添加突变体,如下所示:
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();
希望它能:)
TA贡献1804条经验 获得超7个赞
解决
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]);
}
TA贡献1802条经验 获得超5个赞
用于使用内置方法,如下所示:whereRaw
$users = DB::table('users') ->select('latitude', 'longitude', DB::raw('SQRT(POW(69.1 * (lattitude - 24.900110), 2) + POW(69.1 * (67.099760 -longitude) * COS(latitude / 57.3), 2)) AS distance')) ->whereRaw('SQRT(POW(69.1 * (? - 24.900110), 2) + POW(69.1 * (67.099760 -?) * COS(? / 57.3), 2)) < 25', array($latitude, $longitude, $latitude)) ->orderBy('distance') ->paginate(10) ->get();
- 4 回答
- 0 关注
- 71 浏览
添加回答
举报