Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

rowanwins's avatar

Saving point data to mySQL

Hi all,

I've created a point column in my MySQL database, Im now trying to pass data to that column from a form but I keep getting error messages "Numeric value out of range: 1416 Cannot get geometry object from data you send to the GEOMETRY field"

I've found a reference here that seems to suggest I need to pass the results from my field back as raw although im not quite sure where to implement it. Would I do that in that in my model or my controller?

Any tips would be greatly appreciated. Thanks, Rowan

0 likes
7 replies
rowanwins's avatar

bump.... anyone?

I found another reference here but still can't get things working.

I think I need to add something like this to my model. Do I then to somehow reference that in my Controller?

public function beforeSave($value){
     $this->point = DB::raw("$value");
 }

Thanks

bobbybouwmann's avatar

Show us your migration and how you add data to the database! We can't guess on how you have setup your code

rowanwins's avatar

My migration

 public function up()
    {    
        DB::statement("ALTER TABLE bushwalks ADD COLUMN startPoint POINT"); 
    }

My controller

    public function store()
    {
        $input = Request::all();
        Bushwalk::create($input);
        return $input;
    }

The Model

class Bushwalk extends Model {

    protected $fillable = [
        'title',
        'description',
        'startPoint'
    ];
}

What is being captured in the field

POINT("1 2")

The form submission works fine with all my other fields until I try and enter data into the spatial column :(

Thanks

rowanwins's avatar

Thanks @bobbybouwmann that seems to have done the trick. My final code looks something like this.

public function store()
{
    $bushWalk = Bushwalk::create(Request::except('startPoint'));
        $myPoint = Request::input('startPoint');
        $bushWalk->startPoint = \DB::raw($myPoint);
        $bushWalk->save();
} 

Now to extract the data out .... :)

rowanwins's avatar

For anyone interested in extracting data from spatial columns in MySql try something like this

$myData = \DB::table('bushwalks')->select((\DB::raw('AsText(columnName)')))->where('id',$id)->get();

The important part is the select part which includes a raw db statement. You'll see I've added 'AsText(columnName)' . This AsText statement helps retrieve the data in text format rather than the binary format in which it is normally stored.

For more info take a look at http://dev.mysql.com/doc/refman/5.6/en/fetching-spatial-data.html

Hope this helps someone!

3 likes

Please or to participate in this conversation.