2016-03-19 16 views
0

Ich habe eine Tabelle wie this-Laravel Query Builder - Abfrage mit speziellem Datentyp

enter image description here

Also, ich habe einen speziellen Datentyp (POINT) für user_location.

Die RAW-Auswahlabfrage ist wie this-

SELECT 
    id, 
    full_name, 
    website, 
    X(user_location) AS "latitude", 
    Y(user_location) AS "longitude", 
    (
    GLength(
     LineStringFromWKB(
     LineString(
      user_location, 
      GeomFromText('POINT(51.5177 -0.0968)') 
     ) 
    ) 
    ) 
) 
    AS distance 
FROM users 
    ORDER BY distance ASC; 

Und das Ergebnis ist -

enter link description here

ich es in Laravel mit Query-Builder verwenden möchten, so dass (51.5177-0.0968) diese 2 Punkte können von Benutzereingaben kommen.

Was ich is-

DB::table('users') 
    ->select(
      id, 
      full_name, 
      website, 
      DB::raw('X(user_location) AS "latitude"'), 
      DB::raw('Y(user_location) AS "longitude"'), 
      DB::raw('(
         GLength(
          LineStringFromWKB(
          LineString(
           user_location, 
           GeomFromText('POINT(51.5177 -0.0968)') 
          ) 
         ) 
         ) 
        ) 
         AS distance') 
      ) 
    ->orderBy('distance', 'asc') 
    ->get(); 

getan haben, aber es funktioniert nicht.

Kann mir bitte jemand helfen?

Antwort

1

Ich denke, du bist ziemlich nah dran. Versuchen Sie dies:

DB::table('users') 
    ->select(
     'id', 
     'full_name', 
     'website', 
     DB::raw('X(user_location) as latitude'), 
     DB::raw('Y(user_location) as longitude'), 
     DB::raw('(
       GLength(
        LineStringFromWKB(
        LineString(
         user_location, 
         GeomFromText(POINT(51.5177 - 0.0968)) 
        ) 
       ) 
       ) 
      ) 
       as distance') 
    ) 
    ->where('status', '<>', 1) 
    ->orderBy('distance', 'asc') 
    ->get(); 
+0

Hallo, ein kleines Update, können Sie bitte einen Blick? –