How to seed a geographical POINT using Laravel / MySQL

Robert Coster

Robert Coster

23 July 2021, 12:02

How to seed a geographical POINT using Laravel / MySQL

It's pretty easy to seed a geospatial point to a Laravel DB once you sort through the cruft of people telling you to do it this way or another. You need to use DB::RAW for a start and then ensure that your SQL for DB:RAW is in the format that MYSQL is expecting.

I won't waffle on just to get search / seo hits. This is what you need...

DB::raw("(GEOMFROMTEXT('POINT(54.8765696,-2.9261824)'))")

This is would insert a point on-top of Carlisle station.

In a seeding array this might look something like...

DB::table('places')->insert([['id' => 12, 'location' => DB::raw("(GEOMFROMTEXT('POINT(54.8765696,-2.9261824)'))")]]);

Obviously you'd need a reference to the DB facade for this to work but you've probably already got that....

use Illuminate\Support\Facades\DB;

This answer was trawled up from https://stackoverflow.com/questions/28645394/how-to-insert-spatial-point-values-with-a-query-in-mysql-in-laravel/32619541


More Web Development & Laravel Posts

How to seed a geographical POINT using Laravel / MySQL

Guide: SSL Certificates

Guide: PHP Frameworks

Steps to Dokku and Postgres (with PostGIS) on Ubuntu 16.04

Guide: What is DNS?

Getting mcrypt extension working on PHP 7.3.6 and Homebrew on Macbook

Disabling Events in Laravel Model Observer

Guide: What is a domain name?