2014-11-29 1 views
0

웅변 쿼리에 하버 사인 공식을 사용하는 경우 별칭이 제대로 기록되지이 : 나는 거리로 "쓸 때 Laravel : 내가 쓴

$properties = DB::table('properties') 
    ->join('addresses', 'properties.id_address_fk', '=', 'addresses.id') 
    ->select('properties.id', 'title', 'city', 'price', 'postedat', 
    ('3959 * acos(cos(radians(37)) * cos(radians(lat)) * cos(radians(lng) - radians (-122)) + sin(radians(37)) * sin(radians(lat))) as distance')); 

그러나 오류 통지를 읽기에

, 나는 제대로 Haversie 쿼리를 수용하고 있지 않다 ". 다음은 오류입니다 : 내가 별칭이 필요한 이유

다음
SQLSTATE[42S22]: Column not found: 1054 Unknown column '3959' in 'field list' (SQL: select `properties`.`id`, `title`, `city`, `price`, `postedat`, `3959` as `acos(` from `properties` inner join `addresses` on `properties`.`id_address_fk` = `addresses`.`id` where `propertytype` = house having `distance` < 40 order by `distance` desc limit 5 offset 10) 

이 : 그건 내가 나중에 필터링 할 수 있도록하기 위해 그 별칭이 필요하다

$properties->having('distance', '<', $radius) 
      ->orderBy('distance', 'desc') 
      ->skip(10) 
      ->take(5) 
      ->get(); 

답변

0

Laravel을 주입하기 위해 DB::raw()를 사용해보십시오 이스케이프하지 않고 문자열을 쿼리에 직접 입력합니다.

$properties = DB::table('properties') 
->join('addresses', 'properties.id_address_fk', '=', 'addresses.id') 
->select('properties.id', 'title', 'city', 'price', 'postedat', 
    DB::raw('3959 * acos(cos(radians(37)) * cos(radians(lat)) * cos(radians(lng) - radians (-122)) + sin(radians(37)) * sin(radians(lat))) as distance')); 
+0

그래, 정상입니다. var_dump 대신'dd ($ var)'를 시도하십시오 – lukasgeiter

+0

동일한 긴 표시가 있습니다. 나는 그 표현에 같은 이름의 "속성"이 있기 때문에 일어나고 있다고 생각한다. $ properties = $ properties-> where ('location', '=', $ location); 나도 몰라, 나는 사용자 테이블과 비슷한 쿼리를 수행하고 괜찮 았어. – pavel

+0

DB 원시가 SQL injection에 취약하다는 사실을 읽었습니다. 그렇다면 내 쿼리가 취약한 것입니까? DB :: raw()는 쿼리 빌더가 더 이상 구문 분석하지 않는 임의의 SQL 명령을 만드는 데 사용됩니다. 따라서 SQL 인젝션을 통한 공격을위한 벡터를 생성 할 수 있습니다. – pavel