diff --git a/app/Console/Commands/MigratePlaceDataFromPostgis.php b/app/Console/Commands/MigratePlaceDataFromPostgis.php new file mode 100644 index 00000000..a4e0f38a --- /dev/null +++ b/app/Console/Commands/MigratePlaceDataFromPostgis.php @@ -0,0 +1,84 @@ +exists) { + $this->info('There is no Postgis location data in the table. Exiting.'); + + return 0; + } + + $latitudeColumn = DB::selectOne(DB::raw(" + SELECT EXISTS ( + SELECT 1 + FROM information_schema.columns + WHERE table_name = 'places' + AND column_name = 'latitude' + ) + ")); + + if (! $latitudeColumn->exists) { + $this->error('Latitude and longitude columns have not been created yet'); + + return 1; + } + + $places = Place::all(); + + $places->each(function ($place) { + $this->info('Extracting Postgis data for place: ' . $place->name); + + $place->latitude = $place->location->getLat(); + $place->longitude = $place->location->getLng(); + $place->save(); + }); + + return 0; + } +} diff --git a/database/migrations/2020_10_10_191546_add_latitude-longitude_columns.php b/database/migrations/2020_10_10_191546_add_latitude-longitude_columns.php new file mode 100644 index 00000000..fc3e01cb --- /dev/null +++ b/database/migrations/2020_10_10_191546_add_latitude-longitude_columns.php @@ -0,0 +1,34 @@ +float('latitude')->nullable(); + $table->float('longitude')->nullable(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('places', function (Blueprint $table) { + $table->dropColumn('latitude'); + $table->dropColumn('longitude'); + }); + } +}