0
votes

I want to store this variable to my database, this variable is got result by doing postgis query in laravel, but everytime I tried, I always got error "array to string conversion".

What I used :

  • Laravel v 5.8.17
  • Postgis v 2.5

I have installed postgis in database, I can doing query from pgadmin, but I don't know how to doing it from laravel

$longitude = $request->input('longitude');
$latitude = $request->input('latitude');
$topoint = "SELECT ST_GeomFromText('POINT(".$longitude." ".$latitude.")', 4326)";
$res = DB::select(DB::raw($topoint));

What setting in laravel that must I do to apply the postgis query and that query resulted the geom of that point ?

Update

I have tried to adding new code

$resultfromdb = DB::select(DB::raw($topoint));
$geomresult = json_encode($resultfromdb);
$post->apill_geom = $geomresult;

but it resulted new error, it is

SQLSTATE[XX000]: Internal error: 7 ERROR: parse error - invalid geometry HINT: "[{" <-- parse error at position 2 within geometry (SQL: insert into "e_apill" ("kode", "waktu", "desa", "kecamatan", "kondisi", "apill_geom", "jenis", "lokasi") values (a8a, 2019-05-20 04:46:03, ?, ?, ?, [{"st_geomfromtext":"0101000020E61000000000005E77965B400635AD3816DE1EC0"}], APILL, ?) returning "gid")

What must I do to get this value 0101000020E61000000000005E77965B400635AD3816DE1EC0 on the variable $geomresult

1

1 Answers

0
votes

I think I can close this question, cause now I have find my answer, the code I added is

$longitude = $request->input('longitude');
$latitude = $request->input('latitude');
$topoint = "SELECT ST_GeomFromText('POINT(".$longitude." ".$latitude.")', 4326)";
$res = DB::select(DB::raw($topoint));
$finalres = $res[0]->st_geomfromtext;

$res produced json array so I cannot input it to the field of database, and I must parse it through last code, and yeah it's worked, thanks for all support