How to get Distance in KM in this query

后端 未结 3 1093
花落未央
花落未央 2021-01-29 14:59
$salons = Salon::select(\'salons.*\')
    ->selectRaw(\'( 6371* acos( cos( radians(?) ) *
                   cos( radians( lat ) )
                   * cos( radians(          


        
3条回答
  •  执笔经年
    2021-01-29 15:19

    Try below query

    $data = DB::table('salons AS S')
                    ->selectRaw("
                            ( FLOOR(6371 * ACOS( COS( RADIANS( '$lat' ) ) * COS( RADIANS( S.lat ) ) * COS( RADIANS( S.lng ) - RADIANS( '$lng' ) ) + SIN( RADIANS( '$lat' ) ) * SIN( RADIANS( S.lat ) ) )) ) distance")
                    ->havingRaw("distance < 25")
                    ->where("category_Id" , "=" , $id)
                    ->get();
    

    $lat, $lng are variables.

提交回复
热议问题