Last active
August 29, 2015 14:04
-
-
Save furkantektas/362bb23173824e5535bd to your computer and use it in GitHub Desktop.
Point Accessors for Eloquent (Laravel)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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