- How do I install wgirhad/geophp in a Laravel application?
- Install it with Composer using composer require wgirhad/geophp. Laravel package auto-discovery registers the service provider, so you usually do not need to edit config/app.php manually. If the package ships a config file, publish it with the vendor:publish command to adjust the earth radius or default units. After that you can call the geo helpers from your controllers, jobs, or Eloquent queries.
- Which Laravel and PHP versions does wgirhad/geophp support?
- Check the require section of the package's composer.json for the exact Laravel and PHP version constraints before installing. It is a lightweight package without a full GIS stack, so it generally installs on modern Laravel releases that meet its PHP requirement. If you are on an older Laravel version, verify compatibility first or test in a staging branch.
- How does the distance calculation work?
- The package uses the Haversine formula to calculate the great-circle distance between two latitude and longitude pairs. It relies on a configurable earth-radius constant, so results stay consistent across your application. You get a numeric distance back in your chosen unit rather than needing to implement the trigonometry yourself.
- How can I filter Eloquent models within a radius?
- Use the radius filtering helpers to build a query around a center point and a maximum distance. A common pattern is to first apply a bounding box to narrow candidates using indexed latitude and longitude columns, then apply the precise distance calculation. This keeps queries fast on large tables while still returning accurate radius results.
- What distance units are supported and how do I convert between them?
- The package includes unit conversion helpers for kilometers, miles, and meters. You can calculate in one unit and convert the result to another without writing your own multipliers. Keep the unit consistent within a query or filter so bounding boxes and distance comparisons use the same scale.
- How do I generate a bounding box for a map viewport or database query?
- The bounding box helper takes a center coordinate and a radius or span and returns north, south, east, and west bounds. You can use those bounds in a whereBetween clause on latitude and longitude columns to prefilter rows before exact distance checks. This is useful for map viewports where you only want records visible in the current frame.
- Can I change the earth radius used for calculations?
- Yes, the earth-radius constant is configurable so you can match the model your application expects. Most apps use the mean earth radius, but you can adjust it if you need consistency with another system. Changing it affects distance and radius-filter results, so keep the same value across environments.
- Does wgirhad/geophp require PostGIS or MySQL spatial functions?
- No, the package performs geographic math in PHP and works with ordinary latitude and longitude columns. That means you do not need PostGIS, MySQL spatial indexes, or a full GIS extension to get started. For large datasets, still add normal composite indexes on latitude and longitude and use bounding boxes to reduce rows scanned.
- How should I test geographic calculations in my Laravel app?
- Use known coordinate pairs with expected distances and assert results within a small tolerance, since floating-point math and earth-radius choices can cause tiny differences. Run tests against an in-memory SQLite database for Eloquent radius filters and freeze the earth-radius config in your test environment. Also test edge cases like points near the equator, antimeridian, and poles.
- What are alternatives to wgirhad/geophp for geospatial work in Laravel?
- If you only need distance, bounding boxes, radius filtering, and unit conversion, this package keeps dependencies small. For full geometry parsing or format support such as WKT, GeoJSON, KML, or GPX, consider a dedicated geospatial library like GeoPHP or brick/geo. For database-native spatial queries, look at packages that integrate PostGIS or MySQL spatial types.