微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

编写 Laravel Migration 以创建具有数据类型 point 的 postgres 表列,用于存储纬度经度坐标

如何解决编写 Laravel Migration 以创建具有数据类型 point 的 postgres 表列,用于存储纬度经度坐标

如何编写laravel 迁移来创建点数据类型的列而无需postGIS 扩展

解决方法

<?php
        namespace App\Grammer;

        use Illuminate\Database\Schema\Grammars\PostgresGrammar;
        use Illuminate\Support\Fluent;

        /**
        * Extended version of PostgresGrammar with
        * support of 'point' data type in Postgres.
        */
        class ExtendedPostgresGrammar extends PostgresGrammar
        {

            /**
            * Create the column definition for a spatial Point type.
            *
            * @param  \Illuminate\Support\Fluent  $column
            * @return string
            */
            protected function typePoint(Fluent $column)
            {
                return $column->type;
            }

    }
My Migration is
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use App\Grammer\ExtendedPostgresGrammar;

class UpdateTableAddressesAddColumnPoints extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        // register new grammar class
        DB::connection()->setSchemaGrammar(new ExtendedPostgresGrammar());
        $schema = DB::connection()->getSchemaBuilder();
        $schema->table('addresses',function (Blueprint $table) {
            $table->point('geo_location')->nullable(); 
           
         });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('addresses',function (Blueprint $table) {
            $table->dropColumn('geo_location');
        });
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。