Last active
April 12, 2025 13:51
-
-
Save monkeyphysics/8fa32b4bd79e185bc0a42b6b2ecab427 to your computer and use it in GitHub Desktop.
Laravel MySQL Point Cast class
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 | |
namespace App\Casts; | |
use Illuminate\Support\Facades\DB; | |
use Illuminate\Contracts\Database\Eloquent\CastsAttributes; | |
// todo: make it even better by implementing a Point class and working with that | |
class Point implements CastsAttributes | |
{ | |
public function get($model, $key, $value, $attributes) | |
{ | |
// https://stackoverflow.com/questions/37467050/convert-mysqls-point-to-text-in-php | |
return unpack('x4/corder/Ltype/dlng/dlat', $value); | |
} | |
public function set($model, $key, $value, $attributes) | |
{ | |
return DB::raw(sprintf('POINT(%f, %f)', $value['lng'], $value['lat'])); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
My example for PHP 8 and MySQL 8 with SRID 4326: