{"id":16227032,"url":"https://github.com/matanyadaev/laravel-eloquent-spatial","last_synced_at":"2026-02-27T01:15:42.919Z","repository":{"id":39697994,"uuid":"328411144","full_name":"MatanYadaev/laravel-eloquent-spatial","owner":"MatanYadaev","description":"Laravel Eloquent spatial package.","archived":false,"fork":false,"pushed_at":"2025-02-26T15:54:01.000Z","size":316,"stargazers_count":358,"open_issues_count":5,"forks_count":52,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-04-04T11:41:10.991Z","etag":null,"topics":["eloquent","laravel","php","spatial"],"latest_commit_sha":null,"homepage":"","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/MatanYadaev.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"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}},"created_at":"2021-01-10T15:10:02.000Z","updated_at":"2025-04-01T04:33:31.000Z","dependencies_parsed_at":"2024-03-13T23:31:16.001Z","dependency_job_id":"5824f00f-e34a-4b2f-ab85-92cdafece683","html_url":"https://github.com/MatanYadaev/laravel-eloquent-spatial","commit_stats":{"total_commits":183,"total_committers":9,"mean_commits":"20.333333333333332","dds":"0.13661202185792354","last_synced_commit":"bf6a295c32e83d0e77c9d404be88d452e8e78fd7"},"previous_names":[],"tags_count":38,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MatanYadaev%2Flaravel-eloquent-spatial","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MatanYadaev%2Flaravel-eloquent-spatial/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MatanYadaev%2Flaravel-eloquent-spatial/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MatanYadaev%2Flaravel-eloquent-spatial/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/MatanYadaev","download_url":"https://codeload.github.com/MatanYadaev/laravel-eloquent-spatial/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248448631,"owners_count":21105334,"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":["eloquent","laravel","php","spatial"],"created_at":"2024-10-10T12:51:29.736Z","updated_at":"2026-02-27T01:15:42.913Z","avatar_url":"https://github.com/MatanYadaev.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Laravel Eloquent Spatial\n\n[![Latest Version on Packagist](https://img.shields.io/packagist/v/matanyadaev/laravel-eloquent-spatial.svg?style=flat-square)](https://packagist.org/packages/matanyadaev/laravel-eloquent-spatial)\n![Tests](https://github.com/matanyadaev/laravel-eloquent-spatial/actions/workflows/pest.yml/badge.svg)\n![Static code analysis](https://github.com/matanyadaev/laravel-eloquent-spatial/actions/workflows/phpstan.yml/badge.svg)\n![Lint](https://github.com/matanyadaev/laravel-eloquent-spatial/actions/workflows/pint.yml/badge.svg)\n[![Total Downloads](https://img.shields.io/packagist/dt/matanyadaev/laravel-eloquent-spatial.svg?style=flat-square)](https://packagist.org/packages/matanyadaev/laravel-eloquent-spatial)\n\n**This Laravel package allows you to easily work with spatial data types and functions.**\n\nSupported databases:\n\n- MySQL 5.7/8\n- MariaDB 10\n- Postgres 12/13/14/15/16 with PostGIS 3.4\n\n## Getting Started\n\n### Installing the Package\n\nYou can install the package via composer:\n\n```bash\ncomposer require matanyadaev/laravel-eloquent-spatial\n```\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-\u003egeometry('location', subtype: 'point')-\u003enullable();\n                $table-\u003egeometry('area', subtype: 'polygon')-\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 the `$fillable` and `$casts` arrays and use the `HasSpatial` trait:\n\n    ```php\n    namespace App\\Models;\n\n    use Illuminate\\Database\\Eloquent\\Model;\n    use MatanYadaev\\EloquentSpatial\\Objects\\Point;\n    use MatanYadaev\\EloquentSpatial\\Objects\\Polygon;\n    use MatanYadaev\\EloquentSpatial\\Traits\\HasSpatial;\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 MatanYadaev\\EloquentSpatial\\Objects\\Polygon;\nuse MatanYadaev\\EloquentSpatial\\Objects\\LineString;\nuse MatanYadaev\\EloquentSpatial\\Objects\\Point;\nuse MatanYadaev\\EloquentSpatial\\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\n## Extension\n\n### Extend Geometry class with macros\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### Extend with custom geometry classes\n\nYou can extend the geometry classes by creating custom geometry classes and add functionality. You can also override existing methods, although it is not recommended, as it may lead to unexpected behavior.\n\n1. Create a custom geometry class that extends the base geometry class. \n\n```php\nuse MatanYadaev\\EloquentSpatial\\Objects\\Point;\n\nclass ExtendedPoint extends Point\n{\n    public function toCustomArray(): array\n    {\n        return 'coordinates' =\u003e [\n            'latitude' =\u003e $this-\u003elatitude,\n            'longitude' =\u003e $this-\u003elongitude\n        ]\n    }\n}\n```\n\n2. Update the geometry class mapping in a service provider file.\n\n```php\nuse App\\ValueObjects\\ExtendedPoint;\nuse Illuminate\\Support\\ServiceProvider;\nuse MatanYadaev\\EloquentSpatial\\EloquentSpatial;\n\nclass AppServiceProvider extends ServiceProvider\n{\n    public function boot(): void\n    {\n        EloquentSpatial::usePoint(ExtendedPoint::class);\n    }\n}\n```\n\n3. Update your model to use the custom geometry class in the `$casts` property or `casts()` method.\n\n```php\nuse App\\ValueObjects\\ExtendedPoint;\nuse Illuminate\\Database\\Eloquent\\Model;\nuse MatanYadaev\\EloquentSpatial\\Traits\\HasSpatial;\n\nclass Place extends Model\n{\n    use HasSpatial;\n    \n    protected $casts = [\n        'coordinates' =\u003e ExtendedPoint::class,\n    ];\n    \n    // Or:\n\n    protected function casts(): array\n    {\n        return [\n            'coordinates' =\u003e ExtendedPoint::class,\n        ];\n    }\n}\n```\n\n4. Use the custom geometry class in your code.\n\n```php\nuse App\\Models\\Location;\nuse App\\ValueObjects\\ExtendedPoint;\n\n$place = Place::create([\n    'name' =\u003e 'London Eye',\n    'coordinates' =\u003e new ExtendedPoint(51.5032973, -0.1217424),\n]);\n\necho $place-\u003ecoordinates-\u003etoCustomArray(); // ['longitude' =\u003e -0.1217424, 'latitude' =\u003e 51.5032973]\n```\n\n## Set default SRID\n\nBy default, the SRID is set to 0 (EPSG:0).\nYou can set the default SRID for your application by setting the `SRID` constant in a service provider's `boot` method:\n\n```php\nuse MatanYadaev\\EloquentSpatial\\Enums\\Srid;\nuse Illuminate\\Support\\ServiceProvider;\n\nclass AppServiceProvider extends ServiceProvider\n{\n    public function boot(): void\n    {\n        // Set the default SRID to WGS84 (EPSG:4326)\n        EloquentSpatial::setDefaultSrid(Srid::WGS84);\n    }\n}\n```\n\n## Development\n\nHere are some useful commands for development:\n\n* Run tests: `composer pest:mysql`, `composer pest:mariadb`, `composer pest:postgres`\n* Run tests with coverage: `composer pest-coverage:mysql`\n* Perform type checking: `composer phpstan`\n* Perform code formatting: `composer pint`\n\nBefore running tests, make sure to run `docker-compose up` to start the database container.\n\n## Updates and Changes\n\nFor details on updates and changes, please refer to our [CHANGELOG](CHANGELOG.md).\n\n## License\n\nLaravel Eloquent Spatial is released under The MIT License (MIT). For more information, please see our [License File](LICENSE.md).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmatanyadaev%2Flaravel-eloquent-spatial","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmatanyadaev%2Flaravel-eloquent-spatial","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmatanyadaev%2Flaravel-eloquent-spatial/lists"}