- How do I install jackpopp/geodistance in a Laravel project?
- Run `composer require jackpopp/geodistance` to install the package. No additional Laravel service provider registration is required—it integrates directly with Eloquent. Ensure your database supports geospatial queries (e.g., MySQL SPATIAL indexes or PostgreSQL PostGIS).
- Which Laravel versions does this package support?
- The package is designed for Laravel 5.5+ and leverages Eloquent’s query builder. While it may work on older versions, compatibility isn’t guaranteed, and the last update was in 2018. Test thoroughly if using Laravel 6.x or 7.x.
- Can I use this package with SQLite?
- No, SQLite lacks native geospatial support without custom extensions. The package relies on MySQL’s SPATIAL indexes or PostgreSQL’s PostGIS. If you’re using SQLite, consider alternatives like PHP-only distance calculations (e.g., Haversine formula) or migrate to MySQL/PostgreSQL.
- How do I add a radius-based query to an Eloquent model?
- Use the `near()` method on your model’s query builder: `Model::near($latitude, $longitude, $radius, $unit = 'km')`. Replace `$radius` with your desired distance (e.g., 5 for 5km) and specify `unit` as `'km'` or `'mi'`. Ensure your model’s latitude/longitude columns are indexed spatially.
- What database setup is required for geospatial queries?
- For MySQL, add a SPATIAL index: `ALTER TABLE your_table ADD SPATIAL INDEX (latitude, longitude)`. For PostgreSQL, enable PostGIS (`CREATE EXTENSION postgis`) and add a `GEOGRAPHY(POINT, 4326)` column. SQLite is unsupported without third-party extensions.
- Will this package work in production with high traffic?
- Geospatial queries can be resource-intensive. Test under load to monitor performance, especially with large radius searches. Database-native functions (e.g., MySQL’s `ST_Distance`) are faster than PHP-based calculations. Consider caching frequent queries or optimizing indexes.
- Are there alternatives to jackpopp/geodistance for Laravel?
- Yes, consider `spatie/laravel-geolocation` (actively maintained) or `torann/laravel-geocoder` for geocoding and distance calculations. If you need a lightweight solution, `vinkla/hashid` (for encoding) + custom Haversine logic is another option, though it lacks Eloquent integration.
- How do I handle invalid or missing latitude/longitude values?
- The package doesn’t include built-in validation, so you’ll need to filter or default values manually. For example, add a scope to your model: `public function scopeValidCoordinates($query) { return $query->whereNotNull('latitude')->whereNotNull('longitude'); }`. Test edge cases like NULL or out-of-bounds values.
- Can I use this package for global applications (e.g., queries across timezones or antimeridians)?
- The package uses basic distance formulas, which may introduce errors near the antimeridian or poles. For global accuracy, rely on database-native geospatial functions (e.g., PostgreSQL’s PostGIS) or consider a dedicated geospatial database like MongoDB with GeoJSON support.
- Is jackpopp/geodistance still maintained? Should I use it for new projects?
- The package hasn’t been updated since 2018 and lacks active maintenance. For new projects, evaluate alternatives like `spatie/laravel-geolocation` or `torann/laravel-geocoder`, which are more actively developed. If you proceed, plan for potential future migration.