1- # Laravel Eloquent Spatial
1+ # Laravel Spatial
22
33[ ![ Latest Version on Packagist] ( https://img.shields.io/packagist/v/asanikovich/laravel-spatial.svg?style=flat-square )] ( https://packagist.org/packages/asanikovich/laravel-spatial )
4- [ ![ GitHub Tests Action Status] ( https://img.shields.io/github/actions/workflow/status/asanikovich/laravel-spatial/run-tests.yml?branch=main&label=tests&style=flat-square )] ( https://github.com/asanikovich/laravel-spatial/actions?query=workflow%3Arun-tests+branch%3Amain )
5- [ ![ GitHub Code Style Action Status] ( https://img.shields.io/github/actions/workflow/status/asanikovich/laravel-spatial/fix-php-code-style-issues.yml?branch=main&label=code%20style&style=flat-square )] ( https://github.com/asanikovich/laravel-spatial/actions?query=workflow%3A"Fix+PHP+code+style+issues"+branch%3Amain )
4+ [ ![ GitHub Tests Status] ( https://img.shields.io/github/actions/workflow/status/asanikovich/laravel-spatial/pest.yml?branch=master&label=tests&style=flat-square )] ( https://github.com/asanikovich/laravel-spatial/actions/workflows/pest.yml?query=branch%3Amaster )
5+ [ ![ GitHub Tests Coverage Status] ( https://img.shields.io/codecov/c/github/asanikovich/laravel-spatial?token=E0703O0PPT&style=flat-square )] ( https://github.com/asanikovich/laravel-spatial/actions/workflows/pest-coverage.yml?query=branch%3Amaster )
6+ [ ![ GitHub Code Style Status] ( https://img.shields.io/github/actions/workflow/status/asanikovich/laravel-spatial/phpstan.yml?branch=master&label=code%20style&style=flat-square )] ( https://github.com/asanikovich/laravel-spatial/actions/workflows/phpstan.yml?query=branch%3Amaster )
7+ [ ![ GitHub Lint Status] ( https://img.shields.io/github/actions/workflow/status/asanikovich/laravel-spatial/pint.yml?branch=master&label=lint&style=flat-square )] ( https://github.com/asanikovich/laravel-spatial/actions/workflows/pint.yml?query=branch%3Amaster )
68[ ![ Total Downloads] ( https://img.shields.io/packagist/dt/asanikovich/laravel-spatial.svg?style=flat-square )] ( https://packagist.org/packages/asanikovich/laravel-spatial )
9+ [ ![ Licence] ( https://img.shields.io/packagist/l/asanikovich/laravel-spatial.svg?style=flat-square )] ( https://packagist.org/packages/asanikovich/laravel-spatial )
710
811** This Laravel package allows you to easily work with spatial data types and functions.**
912
10- The latest version, v3, supports Laravel 10 and PHP 8.1+. For Laravel 8 or 9, and PHP 8.0, use v2 .
13+ v1 supports Laravel 8,9 and PHP 8.1+ .
1114
12- This package supports MySQL v8, MySQL v5.7, and MariaDB v10.
15+ This package supports MySQL v8 or v5.7, and MariaDB v10.
1316
1417## Getting Started
1518
@@ -21,12 +24,32 @@ You can install the package via composer:
2124composer require asanikovich/laravel-spatial
2225```
2326
27+ ### Configuration
28+
29+ Default Configuration file includes geometry types mapping:
30+ ``` php
31+ <?php
32+
33+ use ASanikovich\LaravelSpatial\Enums\GeometryType;
34+ use ASanikovich\LaravelSpatial\Geometry;
35+
36+ return [
37+ GeometryType::POINT->value => Geometry\Point::class,
38+ GeometryType::POLYGON->value => Geometry\Polygon::class,
39+ /// ...
40+ ];
41+ ```
42+
2443You can publish the config file with:
2544
2645``` bash
2746php artisan vendor:publish --tag=" laravel-spatial-config"
2847```
2948
49+ If you want you can override custom geometry types mapping:
50+ * globally by config file
51+ * by custom ` $casts ` in your model (top priority)
52+
3053### Setting Up Your First Model
3154
32551 . First, generate a new model along with a migration file by running:
@@ -67,21 +90,19 @@ php artisan vendor:publish --tag="laravel-spatial-config"
6790 php artisan migrate
6891 ```
6992
70- 4. In your new model, fill the `$fillable` and `$ casts` arrays and use the `HasSpatial` trait:
93+ 4. In your new model, fill `$ casts` arrays and use the `HasSpatial` trait (fill the `$fillable` - optional) :
7194
7295 ```php
7396 namespace App\Models;
7497
7598 use Illuminate\Database\Eloquent\Model;
7699 use ASanikovich\LaravelSpatial\Eloquent\HasSpatial;
77- use ASanikovich\LaravelSpatial\Eloquent\SpatialBuilder;
78100 use ASanikovich\LaravelSpatial\Geometry\Point;
79101 use ASanikovich\LaravelSpatial\Geometry\Polygon;
80102
81103 /**
82104 * @property Point $location
83105 * @property Polygon $area
84- * @method static SpatialBuilder<Place > query()
85106 */
86107 class Place extends Model
87108 {
@@ -155,25 +176,9 @@ echo $vacationCity->area->toJson(); // {"type":"Polygon","coordinates":[[[41.907
155176
156177For more comprehensive documentation on the API, please refer to the [ API] ( API.md ) page.
157178
158- ## Tips for Improving IDE Support
159-
160- In order to get better IDE support, you can add a ` query ` method phpDoc annotation to your model:
161-
162- ``` php
163- /**
164- * @method static SpatialBuilder query()
165- */
166- class Place extends Model
167- {
168- // ...
169- }
170- ```
171-
172- Create queries only with the ` query() ` static method:
173-
179+ Create queries only with scopes methods:
174180``` php
175- Place::query()->whereDistance(...); // This is IDE-friendly
176- Place::whereDistance(...); // This is not
181+ Place::whereDistance(...); // This is IDE-friendly
177182```
178183
179184## Extension
@@ -204,18 +209,37 @@ echo $londonEyePoint->getName(); // Point
204209```
205210
206211## Development
212+ Here are some useful commands for development
207213
208- Here are some useful commands for development:
209-
210- * Run tests: ` composer pest `
211- * Run tests with coverage: ` composer pest-coverage `
212- * Perform type checking: ` composer phpstan `
213- * Format your code: ` composer format `
214+ Before running tests run db by docker-compose:
215+ ``` bash
216+ docker-compose up -d
217+ ```
218+ Run tests:
219+ ``` bash
220+ composer run test
221+ ```
222+ Run tests with coverage:
223+ ``` bash
224+ composer run test-coverage
225+ ```
226+ Perform type checking:
227+ ``` bash
228+ composer run phpstan
229+ ```
230+ Format your code:
231+ ``` bash
232+ composer run format
233+ ```
214234
215235## Updates and Changes
216236
217237For details on updates and changes, please refer to our [ CHANGELOG] ( CHANGELOG.md ) .
218238
219239## License
220240
221- Laravel Eloquent Spatial is released under The MIT License (MIT). For more information, please see our [ License File] ( LICENSE.md ) .
241+ Laravel Spatial is released under The MIT License (MIT). For more information, please see our [ License File] ( LICENSE.md ) .
242+
243+ ## Credits
244+
245+ Originally inspired from [ MatanYadaev's laravel-eloquent-spatial package] ( https://github.com/MatanYadaev/laravel-eloquent-spatial ) .
0 commit comments