Richmond Watkins
Richmond Watkins

Reputation: 1370

Using postgis geography functions from laravel.

I have a laravel backend to manage a location plugin of sorts. I am using postgresql with the postgis extension. From the postgres shell I am able to execture postgis functions. However, when I try to use the same functions from within my laravel controller I get undefined column errors. As you can see in my examples I am not trying to query anything from the database. I have two points already loaded into memory that I am trying to find the distance between.

Here is the query:

SELECT ST_Distance(ST_Transform(ST_GeomFromText('POINT(-72.1235 42.3521)',4326),2163),ST_Transform(ST_GeomFromText('LINESTRING(-72.1260 42.45, -72.123 42.1546)', 4326),2163));

From the shell I get the correct output shell output

However, when trying to execute the raw SQL statement from laravel

DB::select ( DB::raw( 'SELECT ST_Distance(ST_Transform(ST_GeomFromText("POINT(-72.1235 42.3521)",4326),2163),ST_Transform(ST_GeomFromText("LINESTRING(-72.1260 42.45, -72.123 42.1546)", 4326),2163))' ) );

laravel output

I have limit knowledge in both laravel and postgresql so it is very likely I am doing something blatantly wrong. However, I have not been able to find my resources on using postgis from laravel. Any help is greatly appreciated!

Upvotes: 2

Views: 2180

Answers (1)

Mike T
Mike T

Reputation: 43642

Simple: use the first SQL example for the second. The second example uses incorrect quoting characters (' and ").

As a general rule for PostgreSQL, use only double quotes (e.g. "MyTable") for database objects like tables, column names, etc., and single quotes (e.g. 'a string') for string literals, like text and character varying (n).

The first example correctly used the string literal 'POINT(-72.1235 42.3521)', but the second used "POINT(-72.1235 42.3521)" which explains why it was looking for a database object with that name, which obviously you don't.

Upvotes: 2

Related Questions