- How do I install and set up stevebauman/location in Laravel?
- Run `composer require stevebauman/location`, then publish the config with `php artisan vendor:publish --tag=location-config`. Configure your preferred driver (e.g., MaxMind or IPAPI) in `config/location.php`, and you’re ready to use `Location::get()` in your code.
- Which geolocation providers does this package support?
- The package supports MaxMind (local or web service), IPAPI, IP2Location, and custom drivers. You can switch providers by updating the `driver` key in the config file without changing your application logic.
- Can I use this package with Laravel middleware for geo-blocking?
- Yes, the package includes `GeoRestrictMiddleware` for route-level geo-blocking. Use it like this: `Route::middleware(['geo.restrict' => ['EU']])->group(...)` to restrict access to specific regions.
- How do I cache geolocation results to reduce API calls?
- Enable caching by setting `cache_enabled` to `true` in the config and configuring `cache_ttl` (time-to-live in minutes). The package uses Laravel’s cache system, so you can integrate with Redis, Memcached, or file caching.
- Does stevebauman/location support IPv6 addresses?
- Support varies by provider. MaxMind handles IPv6 well, but IPAPI may not. Test with your target IPs early, and configure a fallback driver in the config if needed.
- How can I mock geolocation data for testing?
- Use the `Location::fake()` method to simulate geolocation responses in tests. For example: `Location::fake(['countryCode' => 'US', 'ip' => '192.0.2.1'])` ensures consistent test results without hitting external APIs.
- What Laravel versions does this package support?
- The package supports Laravel 8 through 13. For long-term projects, pin your Composer dependency to a specific minor version (e.g., `^7.6`) to avoid unexpected breaking changes.
- How do I handle GDPR/CCPA compliance with stored IP addresses?
- Anonymize IPs post-lookup by setting `Location::get()->ip = null` or use `Location::forget()` to purge cached data. Avoid storing raw IPs unless absolutely necessary, and ensure your config respects data protection laws.
- What’s the best free geolocation provider for high accuracy?
- For free tiers, IPAPI offers decent accuracy but may have rate limits. For higher precision, consider MaxMind’s free GeoLite2 database (local setup) or their commercial web service. Test providers with your expected traffic volume.
- Can I extend the package to add custom geolocation logic?
- Yes, the package supports macros and custom drivers. For example, add a macro like `Location::macro('isEu', fn() => Location::get()->countryCode->startsWith('DE|FR|...'))` for region-specific checks.