jackpopp/geodistance
Laravel/PHP package to calculate geographic distances between coordinates. Supports common formulas and helpers to get miles/kilometers between points, useful for proximity search, radius filtering, and location-based features in apps.
SPATIAL index, PostgreSQL’s PostGIS). Assess whether the existing database supports geospatial queries without major schema changes.ST_Distance) are typically more efficient than PHP-based calculations.Model::near($latitude, $longitude, $radius)), reducing custom query logic. However, ensure the team is comfortable with Eloquent extensions.latitude/longitude columns to models or enabling geospatial indexes. Assess whether this is a breaking change or can be backfilled.NULL, out-of-bounds values) gracefully. The package may not include robust validation by default.SPATIAL index on (latitude, longitude) columns. Enable with:
ALTER TABLE locations ADD SPATIAL INDEX (location_point);
CREATE EXTENSION postgis;
ALTER TABLE locations ADD COLUMN location_point GEOGRAPHY(POINT, 4326);
latitude/longitude columns to target models (if not present).geocoder-php to convert addresses to coordinates).composer require jackpopp/geodistance.config/app.php to include the service provider.near() method:
$nearbyLocations = Location::near($userLat, $userLng, 10)->get();
getDistanceAttribute() if custom distance logic is needed.near() queries with synthetic data.OPTIMIZE TABLE for MySQL).\DB::enableQueryLog();
Location::near($lat, $lng, 5)->get();
dd(\DB::getQueryLog());
INSERT/UPDATE. Test under load and consider batch updates.| Failure Scenario | Impact | Mitigation |
|---|---|---|
| Database geospatial extension missing | Queries fail silently or return incorrect results | Validate database setup during deployment. |
| Missing/Invalid coordinates | NULL or malformed results |
Add model validation (e.g., latitude between -90 and 90). |
| Large radius queries | Database timeouts or locks | Limit maximum radius or use pagination. |
| High query volume | Database overload | Implement rate limiting or caching. |
| Package deprecation | Unmaintained codebase | Fork or migrate to spatie/laravel-geolocation. |
How can I help you explore Laravel packages today?