Skip to content

Instantly share code, notes, and snippets.

@furkantektas
Last active August 29, 2015 14:04
Show Gist options
  • Save furkantektas/362bb23173824e5535bd to your computer and use it in GitHub Desktop.
Save furkantektas/362bb23173824e5535bd to your computer and use it in GitHub Desktop.
Point Accessors for Eloquent (Laravel)
<?php
class ModelName extends Eloquent {
// Ref: http://stackoverflow.com/a/23750526
// Assuming Point is entered as text. Eg: 41,29
public function setLocationAttribute($value) {
$loc = explode(',',$value);
$this->attributes['location'] = DB::raw("ST_GeomFromText('POINT(".$loc[0]." ".$loc[1].")', 4326)");
}
public function getLocationAttribute($value) {
$id = $this->attributes['id'];
$wkt = DB::table('table_name')->find( $id, array(DB::raw('ST_AsText(location) AS location')));
$location = $wkt->location;
return $location;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment