Show the closest stores first.
Which part of this query can't use a ? binding?
Reveal answer
✓ Answer
The asc/desc keyword.
Eloquent performanceLesson 27 of 28
Same expression, now in ORDER BY.
1 min readIntermediateLesson
Show the closest stores first.
Order by the same ST_Distance expression.
$query->orderByRaw(
'ST_Distance(location, ST_SRID(Point(?, ?), 4326)) '.request('direction'),
$coordinates
);
// SQL injection
$direction = strtolower($direction) === 'asc' ? 'asc' : 'desc';
$query->orderByRaw(
'ST_Distance(location, ST_SRID(Point(?, ?), 4326)) '.$direction,
$coordinates
);
Closest stores first, with no way to inject SQL through the direction.

scopeWithinDistanceTo and scopeOrderByDistanceTo both use the precomputed location point column, and the sort direction is whitelisted to asc/desc.
Same expression, now in ORDER BY.
Which part of this query can't use a ? binding?
✓ Answer
The asc/desc keyword.