【问题标题】:Using laravel to create postgres table with point column without postGIS extension使用 laravel 创建带有点列的 postgres 表,没有 postGIS 扩展
【发布时间】:2020-02-26 04:08:32
【问题描述】:

我是 Laravel 和 postgres 的新手。我正在使用没有 postGIS 扩展的 postGres,即使没有 postGIS,仅 postGres 确实允许列的点类型,我在幼虫中的迁移失败并给我这个错误

Illuminate\Database\QueryException : SQLSTATE[42704]: 未定义 对象:7 错误:字符 134 处不存在类型“地理” (SQL:创建表“profiles”(“id” bigserial 主键不为空, “产品 t” varchar(50) 不为空,“详细信息” varchar(1000) 不为空, “xy”地理(点,4326)不为空,“created_at”时间戳(0) 无时区 null, "updated_at" 时间戳(0) 无时区 空))

我的迁移功能代码如下

   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 中创建点列。

谢谢

【问题讨论】:

  • 在架构之后的迁移中使用DB::statement,例如:DB::statement('ALTER TABLE profiles MODIFY xy POINT');
  • 如何更改不存在的表?由于上述错误,未创建表。
  • 我也是 postgress 新手(所以可能没有任何帮助),但如果您手动创建表(通过 DataGrip 或 HeidiSql 等工具),您可以看到 create table 方案查看创建代码并验证它应该是什么?我猜可以让搜索解决方案变得更容易。

标签: php laravel postgresql


【解决方案1】:
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)

    <?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');
            });
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-23
    • 1970-01-01
    • 1970-01-01
    • 2020-07-24
    • 2018-02-27
    • 1970-01-01
    相关资源
    最近更新 更多