Skip to content

Commit 6f6973b

Browse files
committed
Geo ordering
1 parent c036b22 commit 6f6973b

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

src/Query/Builder.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,11 @@ public function orderBy($column, $direction = 1, array $options = []): self
459459
$this->sorts[$column] = $direction;
460460

461461
return $this;
462+
} elseif (is_array($direction)) {
463+
$options = $direction;
464+
$direction = 1;
462465
}
466+
463467
$type = $options['type'] ?? 'basic';
464468

465469
$this->orders[] = compact('column', 'direction', 'type', 'options');
@@ -1548,6 +1552,10 @@ public function orSearchNotFuzzyPrefix($query, mixed $columns = null, $options =
15481552
public function orderByGeo(string $column, array $coordinates, $direction = 1, array $options = []): self
15491553
{
15501554

1555+
if (is_array($direction)) {
1556+
$options = $direction;
1557+
$direction = 1;
1558+
}
15511559
$options = [
15521560
...$options,
15531561
'type' => 'geoDistance',

src/Query/Grammar.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -783,10 +783,16 @@ protected function compileOrders(Builder|BaseBuilder $builder, $orders = []): ar
783783
$orderSettings = [
784784
$column => $order['options']['coordinates'],
785785
'order' => $order['direction'] < 0 ? 'desc' : 'asc',
786-
'unit' => $order['options']['unit'] ?? 'km',
787-
'distance_type' => $order['options']['distance_type'] ?? 'arc',
788786
];
789-
787+
if (! empty($order['options']['unit'])) {
788+
$orderSettings['unit'] = $order['options']['unit'];
789+
}
790+
if (! empty($order['options']['mode'])) {
791+
$orderSettings['mode'] = $order['options']['mode'];
792+
}
793+
if (! empty($order['options']['distance_type'])) {
794+
$orderSettings['distance_type'] = $order['options']['distance_type'];
795+
}
790796
$column = '_geo_distance';
791797
break;
792798
default:

0 commit comments

Comments
 (0)