简体   繁体   中英

Laravel Raw Query - Running this query doesn't select id

i'm trying to run this query but it doesn't find events.id, it actually returns the string "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));

This is the response when i run

"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 ▶}
]

(i need to specify the table name because the actual query is a bit different with an inner join)

Your using the wrong quotes round events.id...

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

You were selecting the literal 'events.id'.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM