【发布时间】:2017-08-12 03:21:15
【问题描述】:
我正在尝试使用 postgis 扩展将几何图形插入到我的 postgresql 数据中。更具体地说,我想插入一个 WKT 线串。这就是我所做的:
$sql = "INSERT INTO myschema.trail (\"name\", \"description\", \"user_id\", \"location_id\", \"source_file\", \"geom\")
VALUES ('".
$p['name']."', '".
$p['description']."', ".
\Auth::user()->user_id.", ".
$p['location_id'].", '".
'uploads/myfile'.",
ST_GeomFromText('$wkt'::text))";
\DB::insert($sql);
当我这样做时,我得到一个错误:
SQLSTATE[42883]:未定义函数:7 错误:函数 st_geomfromtext(text) 不存在
我首先尝试使用参数来执行此操作,但遇到了同样的问题。报错说明生成了如下SQL:
INSERT INTO myschema.trail ("name", "description", "user_id", "location_id", "source_file", "geom")
VALUES ('Test', 'Test', 1, 1, 'uploads/myfile', ST_GeomFromText('LINESTRING(-114.0653326549 49.2872680351, .............'))
当我复制并粘贴生成的 SQL 语句并在 pgAdmin 中运行查询时,查询执行没有问题。我曾尝试在我的应用程序中使用 postgres 用户,以防这是一个权限问题,但这并没有帮助。
如果我删除查询的 postgis 部分,它在我的应用程序中运行良好。无论出于何种原因,我的 Laravel 应用程序无法使用 postgis 函数,但 pgAdmin 可以。
以前有没有人注意到这一点,或者有什么想法可以解决这个问题?谢谢
编辑
我也尝试了以下方法,但我得到了同样的错误:
Trail::create([
'name' => 'test',
'description' => 'test',
'user_id' => 1,
'location' => 1,
'source_file' => 'uploads/myfile',
'geom' => \DB::raw("ST_GeomFromText('$wkt'::text)")
]);
【问题讨论】:
标签: php postgresql laravel postgis