create lat lng validation

This commit is contained in:
2025-10-11 11:48:04 +03:30
parent 21ed605f28
commit bfd53bcd19
3 changed files with 131 additions and 119 deletions

View File

@@ -4,6 +4,7 @@ namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\ModelNotFoundException;
class City extends Model
{
@@ -80,4 +81,30 @@ class City extends Model
{
return $this->belongsToMany('App\Models\EdarateShahri', 'edarate_shahri_be_city')->withPivot('is_primary','id');
}
public static function findCityWithLatLng($lat, $lng): Model|null
{
try {
$city = City::query()
->whereRaw("
ST_Contains(
geometry,
ST_GeomFromText('POINT($lng $lat)', 4326)
)
")
->firstOrFail();
}
catch (ModelNotFoundException $exception) {
$city = City::query()
->orderByRaw("
ST_Distance(
geometry,
ST_GeomFromText('POINT($lng $lat)', 4326)
) ASC
")
->first();
}
return $city;
}
}