create lat lng validation
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user