Skip to content

Commit e616fc0

Browse files
committed
Merge pull request #37 from tarfin-labs/specifying-table-name
Fix spatial queries by explicitly referencing table names
1 parent 582744d commit e616fc0

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

src/Traits/HasSpatial.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ trait HasSpatial
1515
public function scopeSelectDistanceTo(Builder $query, string $column, Point $point): void
1616
{
1717
if (is_null($query->getQuery()->columns)) {
18-
$query->select('*');
18+
$query->select("{$this->getTable()}.*");
1919
}
2020

2121
match (DB::connection()->getDriverName()) {
@@ -59,7 +59,7 @@ public function newQuery(): Builder
5959

6060
$raw = substr($raw, 0, -2);
6161

62-
return parent::newQuery()->addSelect('*', DB::raw($raw));
62+
return parent::newQuery()->addSelect("{$this->getTable()}.*", DB::raw($raw));
6363
}
6464

6565
public function getLocationCastedAttributes(): Collection

tests/HasSpatialTest.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public function it_generates_sql_query_for_selectDistanceTo_scope(): void
1919

2020
// Assert
2121
$this->assertEquals(
22-
expected: "select *, CONCAT(ST_AsText(addresses.$castedAttr, 'axis-order=long-lat'), ',', ST_SRID(addresses.$castedAttr)) as $castedAttr, ST_Distance(ST_SRID($castedAttr, ?), ST_SRID(Point(?, ?), ?)) as distance from `addresses`",
22+
expected: "select `addresses`.*, CONCAT(ST_AsText(addresses.$castedAttr, 'axis-order=long-lat'), ',', ST_SRID(addresses.$castedAttr)) as $castedAttr, ST_Distance(ST_SRID($castedAttr, ?), ST_SRID(Point(?, ?), ?)) as distance from `addresses`",
2323
actual: $query->toSql()
2424
);
2525
}
@@ -36,7 +36,7 @@ public function it_generates_sql_query_for_withinDistanceTo_scope(): void
3636

3737
// 3. Assert
3838
$this->assertEquals(
39-
expected: "select *, CONCAT(ST_AsText(addresses.$castedAttr, 'axis-order=long-lat'), ',', ST_SRID(addresses.$castedAttr)) as $castedAttr from `addresses` where ST_AsText(location) != ? and ST_Distance(ST_SRID($castedAttr, ?), ST_SRID(Point(?, ?), ?)) <= ?",
39+
expected: "select `addresses`.*, CONCAT(ST_AsText(addresses.$castedAttr, 'axis-order=long-lat'), ',', ST_SRID(addresses.$castedAttr)) as $castedAttr from `addresses` where ST_AsText(location) != ? and ST_Distance(ST_SRID($castedAttr, ?), ST_SRID(Point(?, ?), ?)) <= ?",
4040
actual: $query->toSql()
4141
);
4242
}
@@ -54,12 +54,12 @@ public function it_generates_sql_query_for_orderByDistanceTo_scope(): void
5454

5555
// 3. Assert
5656
$this->assertEquals(
57-
expected: "select *, CONCAT(ST_AsText(addresses.$castedAttr, 'axis-order=long-lat'), ',', ST_SRID(addresses.$castedAttr)) as $castedAttr from `addresses` order by ST_Distance(ST_SRID($castedAttr, ?), ST_SRID(Point(?, ?), ?)) asc",
57+
expected: "select `addresses`.*, CONCAT(ST_AsText(addresses.$castedAttr, 'axis-order=long-lat'), ',', ST_SRID(addresses.$castedAttr)) as $castedAttr from `addresses` order by ST_Distance(ST_SRID($castedAttr, ?), ST_SRID(Point(?, ?), ?)) asc",
5858
actual: $queryForAsc->toSql()
5959
);
6060

6161
$this->assertEquals(
62-
expected: "select *, CONCAT(ST_AsText(addresses.$castedAttr, 'axis-order=long-lat'), ',', ST_SRID(addresses.$castedAttr)) as $castedAttr from `addresses` order by ST_Distance(ST_SRID($castedAttr, ?), ST_SRID(Point(?, ?), ?)) desc",
62+
expected: "select `addresses`.*, CONCAT(ST_AsText(addresses.$castedAttr, 'axis-order=long-lat'), ',', ST_SRID(addresses.$castedAttr)) as $castedAttr from `addresses` order by ST_Distance(ST_SRID($castedAttr, ?), ST_SRID(Point(?, ?), ?)) desc",
6363
actual: $queryForDesc->toSql()
6464
);
6565
}
@@ -73,7 +73,7 @@ public function it_generates_sql_query_for_location_casted_attributes(): void
7373

7474
// 2. Act & Assert
7575
$this->assertEquals(
76-
expected: "select *, CONCAT(ST_AsText(addresses.$castedAttr, 'axis-order=long-lat'), ',', ST_SRID(addresses.$castedAttr)) as $castedAttr from `addresses`",
76+
expected: "select `addresses`.*, CONCAT(ST_AsText(addresses.$castedAttr, 'axis-order=long-lat'), ',', ST_SRID(addresses.$castedAttr)) as $castedAttr from `addresses`",
7777
actual: $address->query()->toSql()
7878
);
7979
}

0 commit comments

Comments
 (0)