A Laravel package to get geographical location information from IP addresses.
- PHP 8.1 or higher
- Laravel 10, 11, or 12
Install the package via Composer:
composer require reefki/laravel-geoipOptionally, publish the config file:
php artisan vendor:publish --provider="Reefki\Geoip\GeoipServiceProvider" --tag="config"use Reefki\Geoip\Geoip;
$geoip = Geoip::get('8.8.8.8');
$geoip->driver; // geojs
$geoip->ip; // 8.8.8.8
$geoip->city; // Mountain View
$geoip->region; // California
$geoip->country; // United States
$geoip->country_code; // US
$geoip->continent_code; // NA
$geoip->timezone; // America/Los_Angeles
$geoip->latitude; // 37.751
$geoip->longitude; // -97.822
$geoip->cached; // true or falseIPv6 addresses are also supported:
$geoip = Geoip::get('2001:4860:4860::8888');By default, results are cached. To get realtime data:
Geoip::get('8.8.8.8', cache: false);Get GeoIP data for the current request's IP address:
$geoip = $request->geoip();With anonymized IP (for GDPR compliance):
$geoip = $request->geoip(anonymize: true);Get just the anonymized IP address:
$anonymizedIp = $request->anonymizedIp();
// 8.8.8.8 → 8.8.8.0
// 2001:4860:4860::8888 → 2001:4860:4860::Set the default driver in your .env file:
GEOIP_DEFAULT_DRIVER=geojs
Configure caching behavior:
GEOIP_CACHE_STORE=redis
GEOIP_CACHE_TTL=86400
Configure timeout and retry for API requests:
GEOIP_TIMEOUT=10
GEOIP_RETRY=3
GeoJS is a free service with no API key required.
Geoip::driver('geojs')->get('8.8.8.8');IPData requires an API key. Register for an account and add your key to .env:
IPDATA_API_KEY=your_api_key
Then use the driver:
Geoip::driver('ip-data')->get('8.8.8.8');Note: IPData offers 1,500 free requests per day. Higher usage requires a paid plan.
vendor/bin/phpunitThe MIT License (MIT). Please see License File for more information.