Reputation: 14598
I have a table like this-
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 -
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?
Upvotes: 0
Views: 873
Reputation: 2087
I think you're pretty close. Try this:
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();
Upvotes: 1