Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge pull request #37 from tarfin-labs/specifying-table-name #38

Merged
merged 1 commit into from
Feb 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Traits/HasSpatial.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ trait HasSpatial
public function scopeSelectDistanceTo(Builder $query, string $column, Point $point): void
{
if (is_null($query->getQuery()->columns)) {
$query->select('*');
$query->select("{$this->getTable()}.*");
}

match (DB::connection()->getDriverName()) {
Expand Down Expand Up @@ -59,7 +59,7 @@ public function newQuery(): Builder

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

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

public function getLocationCastedAttributes(): Collection
Expand Down
10 changes: 5 additions & 5 deletions tests/HasSpatialTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function it_generates_sql_query_for_selectDistanceTo_scope(): void

// Assert
$this->assertEquals(
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`",
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`",
actual: $query->toSql()
);
}
Expand All @@ -36,7 +36,7 @@ public function it_generates_sql_query_for_withinDistanceTo_scope(): void

// 3. Assert
$this->assertEquals(
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(?, ?), ?)) <= ?",
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(?, ?), ?)) <= ?",
actual: $query->toSql()
);
}
Expand All @@ -54,12 +54,12 @@ public function it_generates_sql_query_for_orderByDistanceTo_scope(): void

// 3. Assert
$this->assertEquals(
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",
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",
actual: $queryForAsc->toSql()
);

$this->assertEquals(
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",
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",
actual: $queryForDesc->toSql()
);
}
Expand All @@ -73,7 +73,7 @@ public function it_generates_sql_query_for_location_casted_attributes(): void

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