0

I have a table like this-

enter image description here

So, I have a special data type ( POINT ) for user_location.

The RAW select query is like 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;

And the result is -

enter link description here

I want to use it in Laravel with Query builder so that (51.5177 -0.0968) this 2 points can come from user input.

What I have done 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();

But it is not working.

Can anyone please help?

4

1 回答 1

1

我觉得你很接近。尝试这个:

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();
于 2016-03-19T04:33:09.630 回答