Laravel 查询生成器 - 特殊数据类型的查询

Laravel Query Builder - Query with Special Data Type

我有一个table这样的-

所以,我有一个特殊的数据类型 (POINT) user_location

原始select查询是这样的-

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;

结果是-

我想在 Laravel 中将其与查询构建器一起使用,以便 (51.5177 -0.0968) 这 2 个点可以来自用户输入。

我所做的是-

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();

但它不起作用。

有人可以帮忙吗?

我认为你很接近。试试这个:

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();