[英]Laravel condition on a DB::raw
am working on project and i need to order my adverts by distance (by using Latitude and longitude )in adition to this i want to make user abl to limite the number of adverts by ginving the max distance for exemple : if user want to get just the adverts in the 100 mils next to him so i order the advert and return him just the adverts that they are 100 mills away from him or less i have done somme work ordring adverts is allready done but i don't have any idea how to continue 我正在做项目,我需要按距离(通过使用纬度和经度)对广告进行排序,此外,我想让用户abl通过给出最大距离来限制广告的数量:例如,如果用户想得到广告在他旁边的100密耳中,所以我订购广告并退还给他,只是距离他们100或更少的广告,我已经完成了一些工作或整理广告,但是我不知道该如何做继续
public function get_searched(Request $request){
$annonce=DB::table("annonces")
->where('nomAnnonce','like','%'.$request->input("nomAnnonce").'%')
->orderBy(
DB::raw('3959 * acos( cos( radians('.$request->input("lat").') ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians(-'.$request->input("lon").') ) + sin( radians('.$request->input("lat").') ) * sin(radians(latitude)) )')
,'desc')
->get();
echo $annonce;
}
if any body can help me to finde out the why to limite the max distance and thnx 如果有人可以帮助我找出为什么限制最大距离和thnx
Use this: 用这个:
$annonce = DB::table("annonces")
->select('annonces.*')
->selectRaw('3959 * acos( cos( radians(?) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians(-?) ) + sin( radians(?) ) * sin(radians(latitude))) distance',
[$request->input('lat'), $request->input('lon'), $request->input('lat')])
->where('nomAnnonce', 'like', '%'.$request->input("nomAnnonce").'%')
->orderBy('distance', 'desc')
->having('distance', '<=', $request->input('max'))
->get();
As Devon points out, using unfiltered user input is extremely dangerous ( SQL injection ). 正如Devon指出的那样,使用未经过滤的用户输入非常危险( SQL注入 )。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.