vincent15000's avatar

Migration with geography ?

Hello,

How to save coordinates as a point type ?

$table->geography('coordinates', subtype: 'point', srid: 4326)->nullable();

In the database there is only one field and I need to save the latitude and the longitude.

In a form, how to retrieve the coordinates ? Do I need 2 fields ? 1 field ? I really don't know how to do.

Do I have to cast the coordinates field as a JSON field with 2 properties : latitude and longitude ?

Thanks for your help.

V

1 like
3 replies
Glukinho's avatar
Glukinho
Best Answer
Level 30

There are some packages, like this, which seems pretty mature: https://github.com/matanyadaev/laravel-eloquent-spatial

Otherwise you should use something like DB::raw(POINT(100 200)), refer to your database spatial type manual.

It would be nice to see Laravel internal support for such things.

Having simple JSON column seems bad to me as you would want to use DB internal capabilities to calculate area of given polygon or distance between given points. With JSON column you have to calculate them by yourself.

1 like
vincent15000's avatar

Thank you for your help.

I don't have used any package and wrote my own code.

It works fine now ;).

DigitalArtisan's avatar

I’d recommend using PostgreSQL with PostGIS for GIS points.

  • You get real location types (no need to store lat/lon separately)
  • Easy queries like “find points within X distance”
  • Fast with built-in spatial indexing
  • Works well with GIS tools

Basically, it’s much simpler and more powerful than trying to handle coordinates yourself.

Please or to participate in this conversation.