{"id":23746207,"url":"https://github.com/asanikovich/laravel-spatial","last_synced_at":"2025-04-09T22:14:05.327Z","repository":{"id":171797878,"uuid":"646770967","full_name":"asanikovich/laravel-spatial","owner":"asanikovich","description":"Laravel Eloquent spatial package","archived":false,"fork":false,"pushed_at":"2024-11-18T13:47:13.000Z","size":77,"stargazers_count":25,"open_issues_count":4,"forks_count":6,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-09T22:13:59.928Z","etag":null,"topics":["geometry","laravel","laravel-10-package","laravel-8-package","laravel-9-package","laravel-eloquent","laravel-eloquent-models","laravel-package","mariadb","mysql","php","spatial","spatial-functions"],"latest_commit_sha":null,"homepage":"https://asanikovich.github.io/laravel-spatial/","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/asanikovich.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":null,"patreon":null,"open_collective":null,"ko_fi":"asanikovich","tidelift":null,"community_bridge":null,"liberapay":null,"issuehunt":null,"otechie":null,"lfx_crowdfunding":null,"custom":null}},"created_at":"2023-05-29T09:55:42.000Z","updated_at":"2025-03-22T12:36:45.000Z","dependencies_parsed_at":null,"dependency_job_id":"5f1b7ac5-f26c-4a10-8033-cd131d196776","html_url":"https://github.com/asanikovich/laravel-spatial","commit_stats":{"total_commits":10,"total_committers":2,"mean_commits":5.0,"dds":0.09999999999999998,"last_synced_commit":"bfdf2f2eb6d31ddff5613121fd37ee9318ed8f68"},"previous_names":["asanikovich/laravel-spatial"],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/asanikovich%2Flaravel-spatial","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/asanikovich%2Flaravel-spatial/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/asanikovich%2Flaravel-spatial/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/asanikovich%2Flaravel-spatial/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/asanikovich","download_url":"https://codeload.github.com/asanikovich/laravel-spatial/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248119291,"owners_count":21050755,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["geometry","laravel","laravel-10-package","laravel-8-package","laravel-9-package","laravel-eloquent","laravel-eloquent-models","laravel-package","mariadb","mysql","php","spatial","spatial-functions"],"created_at":"2024-12-31T13:19:43.654Z","updated_at":"2025-04-09T22:14:05.306Z","avatar_url":"https://github.com/asanikovich.png","language":"PHP","funding_links":["https://ko-fi.com/asanikovich"],"categories":[],"sub_categories":[],"readme":"# Laravel Spatial\n\n[![Latest Version on Packagist](https://img.shields.io/packagist/v/asanikovich/laravel-spatial.svg?style=flat-square)](https://packagist.org/packages/asanikovich/laravel-spatial)\n[![GitHub Tests Status](https://img.shields.io/github/actions/workflow/status/asanikovich/laravel-spatial/pest.yml?branch=master\u0026label=tests\u0026style=flat-square)](https://github.com/asanikovich/laravel-spatial/actions/workflows/pest.yml?query=branch%3Amaster)\n[![GitHub Tests Coverage Status](https://img.shields.io/codecov/c/github/asanikovich/laravel-spatial?token=E0703O0PPT\u0026style=flat-square)](https://github.com/asanikovich/laravel-spatial/actions/workflows/pest-coverage.yml?query=branch%3Amaster)\n[![GitHub Code Style Status](https://img.shields.io/github/actions/workflow/status/asanikovich/laravel-spatial/phpstan.yml?branch=master\u0026label=code%20style\u0026style=flat-square)](https://github.com/asanikovich/laravel-spatial/actions/workflows/phpstan.yml?query=branch%3Amaster)\n[![GitHub Lint Status](https://img.shields.io/github/actions/workflow/status/asanikovich/laravel-spatial/pint.yml?branch=master\u0026label=lint\u0026style=flat-square)](https://github.com/asanikovich/laravel-spatial/actions/workflows/pint.yml?query=branch%3Amaster)\n[![Total Downloads](https://img.shields.io/packagist/dt/asanikovich/laravel-spatial.svg?style=flat-square)](https://packagist.org/packages/asanikovich/laravel-spatial)\n[![Licence](https://img.shields.io/packagist/l/asanikovich/laravel-spatial.svg?style=flat-square)](https://packagist.org/packages/asanikovich/laravel-spatial)\n\n**This Laravel package allows you to easily work with spatial data types and functions.**\n\n* v2 supports Laravel 10+ and PHP 8.1+\n* v1 supports Laravel 8,9 and PHP 8.1+\n\nThis package supports MySQL v8 or v5.7, and MariaDB v10.\n\n## Getting Started\n\n### Installing the Package\n\nYou can install the package via composer:\n\n```bash\ncomposer require asanikovich/laravel-spatial\n```\n\n### Configuration\n\nDefault Configuration file includes geometry types mapping:\n```php\n\u003c?php\n\nuse ASanikovich\\LaravelSpatial\\Enums\\GeometryType;\nuse ASanikovich\\LaravelSpatial\\Geometry;\n\nreturn [\n    GeometryType::POINT-\u003evalue =\u003e Geometry\\Point::class,\n    GeometryType::POLYGON-\u003evalue =\u003e Geometry\\Polygon::class,\n    /// ...\n];\n```\n\nYou can publish the config file with:\n\n```bash\nphp artisan vendor:publish --tag=\"laravel-spatial-config\"\n```\n\nIf you want you can override custom geometry types mapping:\n* globally by config file\n* by custom `$casts` in your model (top priority)\n\n### Setting Up Your First Model\n\n1. First, generate a new model along with a migration file by running:\n\n   ```bash\n   php artisan make:model {modelName} --migration\n   ```\n\n2. Next, add some spatial columns to the migration file. For instance, to create a \"places\" table:\n\n    ```php\n    use Illuminate\\Database\\Migrations\\Migration;\n    use Illuminate\\Database\\Schema\\Blueprint;\n\n    class CreatePlacesTable extends Migration\n    {\n        public function up(): void\n        {\n            Schema::create('places', static function (Blueprint $table) {\n                $table-\u003eid();\n                $table-\u003estring('name')-\u003eunique();\n                $table-\u003epoint('location')-\u003enullable();\n                $table-\u003epolygon('area')-\u003enullable();\n                $table-\u003etimestamps();\n            });\n        }\n\n        public function down(): void\n        {\n            Schema::dropIfExists('places');\n        }\n    }\n    ```\n\n3. Run the migration:\n\n    ```bash\n    php artisan migrate\n    ```\n\n4. In your new model, fill `$casts` arrays and use the `HasSpatial` trait (fill the `$fillable` - optional):\n\n    ```php\n    namespace App\\Models;\n\n    use Illuminate\\Database\\Eloquent\\Model;\n    use ASanikovich\\LaravelSpatial\\Eloquent\\HasSpatial;\n    use ASanikovich\\LaravelSpatial\\Geometry\\Point;\n    use ASanikovich\\LaravelSpatial\\Geometry\\Polygon;\n\n    /**\n     * @property Point $location\n     * @property Polygon $area\n     */\n    class Place extends Model\n    {\n        use HasSpatial;\n\n        protected $fillable = [\n            'name',\n            'location',\n            'area',\n        ];\n\n        protected $casts = [\n            'location' =\u003e Point::class,\n            'area' =\u003e Polygon::class,\n        ];\n    }\n    ```\n\n### Interacting with Spatial Data\n\nAfter setting up your model, you can now create and access spatial data. Here's an example:\n\n```php\nuse App\\Models\\Place;\nuse ASanikovich\\LaravelSpatial\\Geometry\\Polygon;\nuse ASanikovich\\LaravelSpatial\\Geometry\\LineString;\nuse ASanikovich\\LaravelSpatial\\Geometry\\Point;\nuse ASanikovich\\LaravelSpatial\\Enums\\Srid;\n\n// Create new records\n\n$londonEye = Place::create([\n    'name' =\u003e 'London Eye',\n    'location' =\u003e new Point(51.5032973, -0.1217424),\n]);\n\n$whiteHouse = Place::create([\n    'name' =\u003e 'White House',\n    'location' =\u003e new Point(38.8976763, -77.0365298, Srid::WGS84-\u003evalue), // with SRID\n]);\n\n$vaticanCity = Place::create([\n    'name' =\u003e 'Vatican City',\n    'area' =\u003e new Polygon([\n        new LineString([\n              new Point(12.455363273620605, 41.90746728266806),\n              new Point(12.450309991836548, 41.906636872349075),\n              new Point(12.445632219314575, 41.90197359839437),\n              new Point(12.447413206100464, 41.90027269624499),\n              new Point(12.457906007766724, 41.90000118654431),\n              new Point(12.458517551422117, 41.90281205461268),\n              new Point(12.457584142684937, 41.903107507989986),\n              new Point(12.457734346389769, 41.905918239316286),\n              new Point(12.45572805404663, 41.90637337450963),\n              new Point(12.455363273620605, 41.90746728266806),\n        ]),\n    ]),\n])\n\n// Access the data\n\necho $londonEye-\u003elocation-\u003elatitude; // 51.5032973\necho $londonEye-\u003elocation-\u003elongitude; // -0.1217424\n\necho $whiteHouse-\u003elocation-\u003esrid; // 4326\n\necho $vacationCity-\u003earea-\u003etoJson(); // {\"type\":\"Polygon\",\"coordinates\":[[[41.90746728266806,12.455363273620605],[41.906636872349075,12.450309991836548],[41.90197359839437,12.445632219314575],[41.90027269624499,12.447413206100464],[41.90000118654431,12.457906007766724],[41.90281205461268,12.458517551422117],[41.903107507989986,12.457584142684937],[41.905918239316286,12.457734346389769],[41.90637337450963,12.45572805404663],[41.90746728266806,12.455363273620605]]]}\n```\n\n## Further Reading\n\nFor more comprehensive documentation on the API, please refer to the [API](API.md) page.\n\nCreate queries only with scopes methods:\n```php\nPlace::whereDistance(...); // This is IDE-friendly\n```\n\n## Extension\n\nYou can add new methods to the `Geometry` class through macros.\n\nHere's an example of how to register a macro in your service provider's `boot` method:\n\n```php\nclass AppServiceProvider extends ServiceProvider\n{\n    public function boot(): void\n    {\n        Geometry::macro('getName', function (): string {\n            /** @var Geometry $this */\n            return class_basename($this);\n        });\n    }\n}\n```\n\nUse the method in your code:\n\n```php\n$londonEyePoint = new Point(51.5032973, -0.1217424);\n\necho $londonEyePoint-\u003egetName(); // Point\n```\n\n## Development\nHere are some useful commands for development\n\nBefore running tests run db by docker-compose:\n```bash\ndocker-compose up -d\n```\nRun tests:\n```bash\ncomposer run test\n```\nRun tests with coverage:\n```bash\ncomposer run test-coverage\n```\nPerform type checking:\n```bash\ncomposer run phpstan\n```\nFormat your code:\n```bash\ncomposer run format\n```\n\n## Updates and Changes\n\nFor details on updates and changes, please refer to our [CHANGELOG](CHANGELOG.md).\n\n## License\n\nLaravel Spatial is released under The MIT License (MIT). For more information, please see our [License File](LICENSE.md).\n\n## Credits\n\nOriginally inspired from [MatanYadaev's laravel-eloquent-spatial package](https://github.com/MatanYadaev/laravel-eloquent-spatial).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fasanikovich%2Flaravel-spatial","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fasanikovich%2Flaravel-spatial","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fasanikovich%2Flaravel-spatial/lists"}