0

I am new to Laravel and postgres. I am using postGres without postGIS extension, postGres alone does allow a point type for columns even without postGIS, my migration in the larval fails and gives me this error

Illuminate\Database\QueryException : SQLSTATE[42704]: Undefined object: 7 ERROR: type "geography" does not exist at character 134 (SQL: create table "profiles" ("id" bigserial primary key not null, "produc t" varchar(50) not null, "details" varchar(1000) not null, "xy" geography(point, 4326) not null, "created_at" timestamp(0) without time zone null, "updated_at" timestamp(0) without time zone null))

my code for migration function is below

   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 column is meant to be a simple point type available in postgres, but it appears that laravel translates this into geography type which is available in postGIS and I do not need postGIS in my case.

Please advise how to create a point column in laravel without postGIS.

Thanks

Ahmed
  • 14,503
  • 22
  • 92
  • 150
  • use `DB::statement` within migration after schema like: `DB::statement('ALTER TABLE profiles MODIFY xy POINT');` – Farrukh Ayyaz Oct 30 '19 at 19:54
  • how can you alter a non existing table ? the table is not created because of error above. – Ahmed Oct 30 '19 at 20:12
  • I am also new to postgress(so might not be of any help) but if you create the table manually(via a tool like DataGrip or HeidiSql), can you then see the `create table` scheme to see the creation code and verify what it should be? Could make searching for a solution easier I guess. – Techno Oct 30 '19 at 22:02

1 Answers1

0
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');
            });
        }
    }
sufyan
  • 19
  • 3