{"id":25228938,"url":"https://github.com/ajthinking/laravel-postgis","last_synced_at":"2025-10-26T06:31:19.137Z","repository":{"id":42366873,"uuid":"458527275","full_name":"ajthinking/laravel-postgis","owner":"ajthinking","description":null,"archived":false,"fork":false,"pushed_at":"2022-08-25T09:06:40.000Z","size":254,"stargazers_count":15,"open_issues_count":1,"forks_count":5,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-08-10T11:38:39.140Z","etag":null,"topics":[],"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/ajthinking.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-02-12T13:19:03.000Z","updated_at":"2023-03-17T12:53:20.000Z","dependencies_parsed_at":"2022-09-12T11:10:11.206Z","dependency_job_id":null,"html_url":"https://github.com/ajthinking/laravel-postgis","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ajthinking%2Flaravel-postgis","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ajthinking%2Flaravel-postgis/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ajthinking%2Flaravel-postgis/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ajthinking%2Flaravel-postgis/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ajthinking","download_url":"https://codeload.github.com/ajthinking/laravel-postgis/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":238281142,"owners_count":19446078,"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":[],"created_at":"2025-02-11T10:46:27.704Z","updated_at":"2025-10-26T06:31:18.867Z","avatar_url":"https://github.com/ajthinking.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"Laravel Wrapper for PostgreSQL's Geo-Extension Postgis\n======================================================\n\n![Build Status](https://github.com/ajthinking/laravel-postgis/workflows/Test%20Suite/badge.svg)\n\nFor Laravel 9+, PHP 8+ only\n\n## Features\n\n * Work with geometry classes instead of arrays.\n```php\n$model-\u003emyPoint = new Point(1,2);  //lat, long\n```\n\n* Adds helpers in migrations.\n```php\n$table-\u003epolygon('myColumn');\n```\n\n\n\n## Installation\n```bash\ncomposer require mstaack/laravel-postgis\n```\n\n## Usage\n\nTo start, ensure you have PostGIS enabled in your database - you can do this in a Laravel migration or manually via SQL.\n\n### Enable PostGIS via a Laravel migration\n\nYou need to publish the migration to easily enable PostGIS:\n\n```sh\nphp artisan vendor:publish --provider=\"Ajthinking\\LaravelPostgis\\DatabaseServiceProvider\" --tag=\"migrations\"\n```\n\nAnd then you run the migrations:\n\n```sh\nphp artisan migrate\n```\n\nThese methods are safe to use and will only enable / disable the PostGIS extension if relevant - they won't cause an error if PostGIS is / isn't already enabled.\n\nIf you prefer, you can use the `enablePostgis()` method which will throw an error if PostGIS is already enabled, and the `disablePostgis()` method twhich will throw an error if PostGIS isn't enabled.\n\n### Enable PostGIS manually\n\nUse an SQL client to connect to your database and run the following command:\n\n    CREATE EXTENSION postgis;\n\nTo verify that PostGIS is enabled you can run:\n\n    SELECT postgis_full_version();\n\n### Migrations\n\nNow create a model with a migration by running\n\n    php artisan make:model Location\n\nIf you don't want a model and just a migration run\n\n    php artisan make:migration create_locations_table\n\nOpen the created migrations with your editor.\n\n```PHP\nuse Illuminate\\Database\\Migrations\\Migration;\nuse Ajthinking\\LaravelPostgis\\Schema\\Blueprint;\n\nclass CreateLocationsTable extends Migration {\n\n    /**\n     * Run the migrations.\n     *\n     * @return void\n     */\n    public function up()\n    {\n        Schema::create('locations', function(Blueprint $table)\n        {\n            $table-\u003eincrements('id');\n            $table-\u003estring('name');\n            $table-\u003estring('address')-\u003eunique();\n            $table-\u003epoint('location'); // GEOGRAPHY POINT column with SRID of 4326 (these are the default values).\n            $table-\u003epoint('location2', 'GEOGRAPHY', 4326); // GEOGRAPHY POINT column with SRID of 4326 with optional parameters.\n            $table-\u003epoint('location3', 'GEOMETRY', 27700); // GEOMETRY column with SRID of 27700.\n            $table-\u003epolygon('polygon'); // GEOGRAPHY POLYGON column with SRID of 4326.\n            $table-\u003epolygon('polygon2', 'GEOMETRY', 27700); // GEOMETRY POLYGON column with SRID of 27700.\n            $table-\u003etimestamps();\n        });\n    }\n\n    /**\n     * Reverse the migrations.\n     *\n     * @return void\n     */\n    public function down()\n    {\n        Schema::drop('locations');\n    }\n\n}\n```\n\nAvailable blueprint geometries:\n\n * point\n * multipoint\n * linestring\n * multilinestring\n * polygon\n * multipolygon\n * geometrycollection\n\nother methods:\n\n * enablePostgis\n * disablePostgis\n\n### Models\n\nAll models which are to be PostGis enabled **must** use the *PostgisTrait*.\n\nYou must also define an array called `$postgisFields` which defines\nwhat attributes/columns on your model are to be considered geometry objects. By default, all attributes are of type `geography`. If you want to use `geometry` with a custom SRID, you have to define an array called `$postgisTypes`. The keys of this assoc array must match the entries in `$postgisFields` (all missing keys default to `geography`), the values are assoc arrays, too. They must have two keys: `geomtype` which is either `geography` or `geometry` and `srid` which is the desired SRID. **Note**: Custom SRID is only supported for `geometry`, not `geography`.\n\n```PHP\nuse Illuminate\\Database\\Eloquent\\Model;\nuse Ajthinking\\LaravelPostgis\\Eloquent\\PostgisTrait;\nuse Ajthinking\\LaravelPostgis\\Geometries\\Point;\n\nclass Location extends Model\n{\n    use PostgisTrait;\n\n    protected $fillable = [\n        'name',\n        'address'\n    ];\n\n    protected $postgisFields = [\n        'location',\n        'location2',\n        'location3',\n        'polygon',\n        'polygon2'\n    ];\n\n    protected $postgisTypes = [\n        'location' =\u003e [\n            'geomtype' =\u003e 'geography',\n            'srid' =\u003e 4326\n        ],\n        'location2' =\u003e [\n            'geomtype' =\u003e 'geography',\n            'srid' =\u003e 4326\n        ],\n        'location3' =\u003e [\n            'geomtype' =\u003e 'geometry',\n            'srid' =\u003e 27700\n        ],\n        'polygon' =\u003e [\n            'geomtype' =\u003e 'geography',\n            'srid' =\u003e 4326\n        ],\n        'polygon2' =\u003e [\n            'geomtype' =\u003e 'geometry',\n            'srid' =\u003e 27700\n        ]\n    ]\n}\n\n$linestring = new LineString(\n    [\n        new Point(0, 0),\n        new Point(0, 1),\n        new Point(1, 1),\n        new Point(1, 0),\n        new Point(0, 0)\n    ]\n);\n\n$location1 = new Location();\n$location1-\u003ename = 'Googleplex';\n$location1-\u003eaddress = '1600 Amphitheatre Pkwy Mountain View, CA 94043';\n$location1-\u003elocation = new Point(37.422009, -122.084047);\n$location1-\u003elocation2 = new Point(37.422009, -122.084047);\n$location1-\u003elocation3 = new Point(37.422009, -122.084047);\n$location1-\u003epolygon = new Polygon([$linestring]);\n$location1-\u003epolygon2 = new Polygon([$linestring]);\n$location1-\u003esave();\n\n$location2 = Location::first();\n$location2-\u003elocation instanceof Point // true\n```\n\nAvailable geometry classes:\n\n * Point\n * MultiPoint\n * LineString\n * MultiLineString\n * Polygon\n * MultiPolygon\n * GeometryCollection\n\n\n### Achnowledgements\nThis is a [fork](https://github.com/mstaack/laravel-postgis) from the work of great people. Thanks to :\n- https://github.com/njbarrett\n- https://github.com/phaza\n- https://github.com/mirzap\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fajthinking%2Flaravel-postgis","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fajthinking%2Flaravel-postgis","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fajthinking%2Flaravel-postgis/lists"}