首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用laravel创建没有postGIS扩展的点列postgres表

使用laravel创建没有postGIS扩展的点列postgres表
EN

Stack Overflow用户
提问于 2019-10-30 19:33:10
回答 1查看 1.2K关注 0票数 0

我是拉勒维尔和波斯特格斯的新手。我使用的是没有postGres扩展的postGIS,只有postGres才允许列的点类型,即使没有postGIS,我在幼虫中的迁移失败并给出了这个错误。

照明\数据库\QueryException: SQLSTATE42704:未定义对象:7错误:在字符134处不存在“地理”类型(SQL:创建表“概要”("id“大系列主键不为空,"produc t”varchar(50) null,"details“varchar(1000) null,"xy”地理位置(点,4326)非空,"created_at“时间戳(0)没有时区空,"updated_at”时间戳(0)没有时区空)

我的迁移函数代码如下

代码语言:javascript
运行
复制
   public function up()
    {
        Schema::connection('pgsql')->create('profiles', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('product',50);
            $table->string('details',1000);
            $table->point('xy');
            $table->timestamps();
        });
    }

xy列是postgres中可用的一个简单的点类型,但看起来laravel将其转换为地理类型,这在postGIS中是可用的,在我的例子中不需要postGIS。

请建议如何在不使用postGIS的情况下在laravel中创建点列。

谢谢

EN

回答 1

Stack Overflow用户

发布于 2021-02-22 07:38:41

代码语言:javascript
运行
复制
I was having same issue with Laravel Migration to create data type point for this  you can use Extended version of PostgresGrammar with support of 'point' data type in Postgres

Write Laravel Migration to create postgres table column with data type point for to store latitude longitude (coordinates)

代码语言:javascript
运行
复制
    <?php
            namespace App\PosGress;
    
            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";
                }
    
        }
    ?>
    Laravel Migration will be 
    <?php
    
    use Illuminate\Database\Migrations\Migration;
    use Illuminate\Database\Schema\Blueprint;
    use Illuminate\Support\Facades\Schema;
    use App\PosGress\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
         */`enter code here`
        public function down()
        {
            Schema::table('addresses', function (Blueprint $table) {
                $table->dropColumn('geo_location');
            });
        }
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58632491

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档