title: Save postgis geometry type from geojson
body: <p>I have a <a href="https://leafletjs.com/" rel="nofollow noreferrer">Lealfet</a> map in a Laravel Blade view where the user can draw shapes and markers. These features are registered in a <code>GeoJson</code> object, that I stringify and insert in a hidden <code>textarea</code> with name "geojson" to submit it to the server, within a form.</p>
  
  <p>The problem is that I want to save this data as <code>geometry</code> type in my PostgreSQL database, using the PostGis <a href="https://postgis.net/docs/ST_GeomFromGeoJSON.html" rel="nofollow noreferrer"><code>ST_GeomFromGeoJSON()</code></a> function, but I can't get it to work.</p>
  
  <p>Here is what I tried for now:</p>
  
  <pre><code>$site = new Site;
  $data = $request-&gt;all();
  unset($data['geojson']);
  
  foreach($data as $key=&gt;$d)
  {
     $site-&gt;$key = $d;
  }
  
  $geojson = json_decode($request-&gt;geojson);
  $site-&gt;save();
  
  DB::update('update posha_sites set geom = ST_GeomFromGeoJSON(?)
        WHERE num_site = ?
        AND city_id = ?',
     [$geojson, $request-&gt;num_site, $city_id-&gt;id]
  );
  </code></pre>
  
  <p>Right now I am saving all my data and then trying to insert the <code>geospatial</code> data, as I don't know how I could use a raw query while saving the rest of my data.</p>
  
  <p>When I do that I am getting this error: </p>
  
  <blockquote>
    <p>Object of class stdClass could not be converted to string</p>
  </blockquote>
  
  <hr>
  
  <h1>EDIT</h1>
  
  <p>Here is the whole migration for the posha_sites table:</p>
  
  <pre><code>public function up()
  {
      Schema::create('posha_sites', function (Blueprint $table) {
          $table-&gt;bigIncrements('id');
          $table-&gt;string('name');
          $table-&gt;bigInteger('num_site');
          $table-&gt;string('type', 50)-&gt;nullable();
          $table-&gt;mediumText('adresse')-&gt;nullable();
          $table-&gt;string('cartes_anciennes')-&gt;nullable();
          $table-&gt;string('carte_topo')-&gt;nullable();
          $table-&gt;mediumText('cadastre_remembre')-&gt;nullable();
          $table-&gt;mediumText('cadastre_moderne')-&gt;nullable();
          $table-&gt;mediumText('cadastre_ancien')-&gt;nullable();
          $table-&gt;string('lieu_dit')-&gt;nullable();
          $table-&gt;mediumText('nature_parcelles')-&gt;nullable();
          $table-&gt;mediumText('conditions_acces')-&gt;nullable();
          $table-&gt;string('situation_administrative')-&gt;nullable();
          $table-&gt;string('altitude')-&gt;nullable();
          $table-&gt;string('relief')-&gt;nullable();
          $table-&gt;mediumText('hydrographie')-&gt;nullable();
          $table-&gt;string('geologie')-&gt;nullable();
          $table-&gt;string('vestiges_periode')-&gt;nullable();
          $table-&gt;mediumText('vestiges_nature')-&gt;nullable();
          $table-&gt;mediumText('vestiges_conservation')-&gt;nullable();
          $table-&gt;longText('plans_documents_figures')-&gt;nullable();
          $table-&gt;longText('sources_manuscrites')-&gt;nullable();
          $table-&gt;longText('sources_imprimees')-&gt;nullable();
          $table-&gt;longText('renseignement_oral')-&gt;nullable();
          $table-&gt;longText('bibliographie')-&gt;nullable();
          $table-&gt;longText('histoire')-&gt;nullable();
          $table-&gt;longText('historiographie')-&gt;nullable();
          $table-&gt;longText('description_histoire_monumentale')-&gt;nullable();
          $table-&gt;geometrycollection('geom')-&gt;nullable();
          $table-&gt;string('last_author')-&gt;nullable();
          $table-&gt;integer('tree_id')-&gt;unsigned()-&gt;nullable();
          $table-&gt;integer('parent_id')-&gt;unsigned()-&gt;nullable();
          $table-&gt;integer('city_id')-&gt;unsigned();
          $table-&gt;timestamps();
      });
  }
  </code></pre>
  
  <p>The function <code>ST_GeomFromGeoJSON()</code> is in fact expecting a string so I didn't decode my <code>$geojson</code> variable:</p>
  
  <pre><code>$geojson = $request-&gt;geojson;
  </code></pre>
  
  <p>instead of:</p>
  
  <pre><code>$geojson = json_decode($request-&gt;geojson);
  </code></pre>
  
  <p>But I still get an error:</p>
  
  <blockquote>
    <p>SQLSTATE[XX000]: Internal error:</p>
    
    <p>7 ERROR: invalid GeoJson representation (SQL: update posha_sites set geom = ST_GeomFromGeoJSON({"type":"FeatureCollection","features":[{"type":"Feature","properties":{},"geometry":{"type":"Point","coordinates":[-0.229114,44.564488]}}]}) WHERE num_site = 248 AND city_id = 5)</p>
  </blockquote>
  
  <p>Yet I tested my geojson in an online geojson validator and it seems correct.</p>