torann/geoip
Laravel GeoIP package to determine a visitor’s location and currency from their IP address. Supports multiple GeoIP services/drivers via configuration, with publishable config and upgrade guidance. Useful for geo-targeting, localization, and analytics.
composer require torann/geoip
php artisan vendor:publish --provider="Torann\GeoIP\GeoIPServiceProvider" --tag="config"
config/geoip.php. Since MaxMind’s free service is no longer bundled by default, you’ll need to either:
service to 'maxmind_database' and provide the path to your .mmdb file), oripapi.com, ipgeolocation.io, ipinfo.io) — configure your API key in the services section.>>> GeoIP::getLocation('8.8.8.8');
GeoIP::getLocation() in a helper or service class to normalize location data across your app (e.g., app/helpers.php or app/Services/GeoLocation.php).'cache' => true in config) to avoid repeated lookups per request or session. For high-traffic apps, consider extending the cache_lifetime or using a dedicated cache driver.app('geoip')->setIp($request->ip());
$location = app('geoip')->getLocation();
$request->merge(['country' => $location->country]);
config/geoip.php (e.g., try MaxMind DB first, fallback to IP-API). Use GeoIP::getLocation($ip, 'service_name') to target specific services when needed.GeoIP::getCurrency() and GeoIP::getLanguage() for localization — ideal for e-commerce or international apps.null service will cause runtime errors. Always publish config first, especially in staging/production.getLocation() defaults to $request->ip() if no IP is passed. Ensure your app correctly handles proxies/load balancers — verify trustedproxies and TrustProxies middleware if internal IPs (e.g., 192.168.x.x) are used internally.php artisan geoip:update).GeoIP facade in tests to avoid network calls:
GeoIP::shouldReceive('getLocation')->once()->andReturn((object)['country' => 'US']);
GeoIP service by binding your own implementation in AppServiceProvider.GeoIP class to add custom logic (e.g., IP validation, custom fallbacks) and register as a singleton.'log_errors' => true in config) to capture failed lookups — especially useful when tracking down API key issues or rate limiting.How can I help you explore Laravel packages today?