Laravel Raw Query - 运行此查询不会选择ID

时间:2017-06-11 16:28:44

标签: php mysql laravel

我正在尝试运行此查询,但它找不到events.id,它实际上返回字符串“events.id”。

 $events = \DB::select(\DB::raw("select 'events.id', `publish_date`, `event_name`, `information`, `latitude`, `longitude`, 111.045 * DEGREES(ACOS(COS(RADIANS(?))
                  * COS(RADIANS(`latitude`))
                  * COS(RADIANS(`longitude`) - RADIANS(?))
                  + SIN(RADIANS(?))
                  * SIN(RADIANS(`latitude`))))
                  AS `distance_in_km`
                  FROM `events` as `tbl`
                  where (`publish_date` between ? and ?)
                  and (`event_name` like ? or `information` like ?)
                  and (`paid` = 1)
                  having (`distance_in_km` <= ?)
                  ORDER BY `distance_in_km` ASC
                  LIMIT ?,?"), array($lat, $lng, $lat, $date_from, $date_to, $search_term, $search_term, $distance, $page, intval($page)+20));

这是我运行时的回复

  

“return dd($ events)”

array:14 [▼
  0 => {#221 ▼
    +"events.id": "events.id"
    +"publish_date": "2017-05-31"
    +"event_name": "asdfasdfas"
    +"information": "sdfgsdfgs"
    +"latitude": 34.6921597
    +"longitude": 5.0225571
    +"distance_in_km": 1.4213115203631
  }
  1 => {#93 ▶}
  2 => {#225 ▶}
  3 => {#222 ▶}
  4 => {#224 ▶}
  5 => {#226 ▶}
  6 => {#227 ▶}
  7 => {#228 ▶}
  8 => {#229 ▶}
  9 => {#230 ▶}
  10 => {#231 ▶}
  11 => {#232 ▶}
  12 => {#233 ▶}
  13 => {#234 ▶}
]

(我需要指定表名,因为实际查询与内部联接有点不同)

1 个答案:

答案 0 :(得分:1)

你使用错误的引号围绕events.id ......

$events = \DB::select(\DB::raw("select `events.id`, `publish_date`,

您选择了文字'events.id'。