Skip to content

Commit b00b225

Browse files
lukeviMatanYadaev
andauthored
Add HasSpatial trait (#75)
* Reusable trait rather than adding a method * corrections from PR review - update readme instruction - restore phpdoc ide hint - trailing newline for phpcs * Add HasSpatial trait #75 - typo & cs-fixer fix * Update README.md Co-authored-by: Matan Yadaev <[email protected]>
1 parent 9360923 commit b00b225

File tree

3 files changed

+19
-17
lines changed

3 files changed

+19
-17
lines changed

README.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ Run the migration:
5656
php artisan migrate
5757
```
5858

59-
Fill the `$fillable` and `$casts` arrays and add custom eloquent builder to your new model:
59+
Fill the `$fillable` and `$casts` arrays and use the `HasSpatial` trait in your new model:
6060

6161
```php
6262
namespace App\Models;
@@ -65,6 +65,7 @@ use Illuminate\Database\Eloquent\Model;
6565
use MatanYadaev\EloquentSpatial\SpatialBuilder;
6666
use MatanYadaev\EloquentSpatial\Objects\Point;
6767
use MatanYadaev\EloquentSpatial\Objects\Polygon;
68+
use MatanYadaev\EloquentSpatial\Traits\HasSpatial;
6869

6970
/**
7071
* @property Point $location
@@ -73,6 +74,8 @@ use MatanYadaev\EloquentSpatial\Objects\Polygon;
7374
*/
7475
class Place extends Model
7576
{
77+
use HasSpatial;
78+
7679
protected $fillable = [
7780
'name',
7881
'location',
@@ -83,11 +86,6 @@ class Place extends Model
8386
'location' => Point::class,
8487
'area' => Polygon::class,
8588
];
86-
87-
public function newEloquentBuilder($query): SpatialBuilder
88-
{
89-
return new SpatialBuilder($query);
90-
}
9189
}
9290
```
9391

src/Traits/HasSpatial.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace MatanYadaev\EloquentSpatial\Traits;
4+
5+
use MatanYadaev\EloquentSpatial\SpatialBuilder;
6+
7+
trait HasSpatial
8+
{
9+
public function newEloquentBuilder($query): SpatialBuilder
10+
{
11+
return new SpatialBuilder($query);
12+
}
13+
}

tests/TestModels/TestPlace.php

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use MatanYadaev\EloquentSpatial\Objects\Polygon;
1414
use MatanYadaev\EloquentSpatial\SpatialBuilder;
1515
use MatanYadaev\EloquentSpatial\Tests\TestFactories\TestPlaceFactory;
16+
use MatanYadaev\EloquentSpatial\Traits\HasSpatial;
1617

1718
/**
1819
* @property Point $point
@@ -30,7 +31,7 @@
3031
*/
3132
class TestPlace extends Model
3233
{
33-
use HasFactory;
34+
use HasFactory, HasSpatial;
3435

3536
protected $fillable = [
3637
'address',
@@ -55,16 +56,6 @@ class TestPlace extends Model
5556
'point_with_line_string_cast' => LineString::class,
5657
];
5758

58-
/**
59-
* @param $query
60-
* @return SpatialBuilder<TestPlace>
61-
*/
62-
public function newEloquentBuilder($query): SpatialBuilder
63-
{
64-
// @phpstan-ignore-next-line
65-
return new SpatialBuilder($query);
66-
}
67-
6859
protected static function newFactory(): TestPlaceFactory
6960
{
7061
return new TestPlaceFactory;

0 commit comments

Comments
 (0)