Handling Mysql Spatial datatypes in Laravel Eloquent ORM

后端 未结 2 1579
醉酒成梦
醉酒成梦 2021-02-12 12:49

How to handle mysql spatial datatypes in eloquent ORM?, This include how to create migration, insert spatial data and performing spatial query\'s. If there is not actual solutio

2条回答
  •  故里飘歌
    2021-02-12 13:26

    It is available to use https://github.com/grimzy/laravel-mysql-spatial

    you may use:

    namespace App;
    
    use Illuminate\Database\Eloquent\Model;
    use Grimzy\LaravelMysqlSpatial\Eloquent\SpatialTrait;
    
    /**
     * @property \Grimzy\LaravelMysqlSpatial\Types\Point $location
     */
    class Place extends Model
    {
        use SpatialTrait;
    
        protected $fillable = [
            'name',
        ];
    
        protected $spatialFields = [
            'location',
        ];
    }
    

    then you are able to run queries on the 'location' field.

    to store model you may use:

    $place1 = new Place();
    $place1->name = 'Empire State Building';
    $place1->location = new Point(40.7484404, -73.9878441);
    $place1->save();
    

    to retrieving a model you should use:

    $place2 = Place::first();
    $lat = $place2->location->getLat(); // 40.7484404
    $lng = $place2->location->getLng(); // -73.9878441
    

提交回复
热议问题