midnite81/geolocation
Laravel package for IP-based geolocation with configurable drivers. Look up a visitor's location from their IP address—country, city, region, and coordinates—through a simple facade, making it easy to localize content, currency, and redirects.
composer require midnite81/geolocation. If upgrading from v3, do not skip the upgrade notes — v4 is a breaking release (Ip2Location was added as a driver, which almost certainly means the config shape, driver binding, or default service contract changed).config/geolocation.php after running:
php artisan vendor:publish --provider="Midnite81\GeoLocation\GeoLocationServiceProvider"
The driver/default key is where the breaking change will bite you: v3 configs will not map cleanly to v4.use Midnite81\GeoLocation\Contracts\Services\GeoLocation;
public function show(Request $request, GeoLocation $geo)
{
$location = $geo->getLocation($request->ip());
// $location->getCountry(), ->getCity(), etc.
}
(Confirm the exact contract/interface name from the README — this is the single biggest v3→v4 break.)Contracts directory, and the Drivers directory. Those three tell you everything about how to switch providers and what capabilities each supports.IP2LOCATION, MAXMIND, an API-based driver, etc. per environment in .env and let the config default key resolve. This lets local/dev use a no-database stub while production uses a real provider.
// config/geolocation.php (v4 shape — verify keys against README)
'default' => env('GEOLOCATION_DRIVER', 'ip2location'),
'drivers' => [
'ip2location' => [
'database' => env('IP2LOCATION_DB_PATH'),
'mode' => env('IP2LOCATION_MODE', 'bin'),
],
],
.env change, not a code change.LocationService over the package so IP normalization, caching, and fallbacks live in one place and are testable. This is especially important now that v4 adds a second real driver whose return objects may differ.return Cache::remember("geo:{$ip}", now()->addDay(), fn () => $geo->getLocation($ip));
Note that mobile IPs churn; consider shorter TTLs for cellular ranges or cache only country-level data.Cache to avoid per-request hits.composer.lock for the new dependency, and grep your codebase for Midnite81\GeoLocation\ imports — any concrete class references will likely need to become contract references..BIN) database must be downloaded and kept on disk, and the path supplied in config. Missing/expired DBs fail silently or throw — assert the file exists at boot or in a health check.127.0.0.1, 10.x, 192.168.x will return null or an exception depending on the driver. Short-circuit these in your wrapper before calling the package so dev and staging don’t blow up.php artisan config:clear after changing drivers, and check config('geolocation.default') in artisan tinker first.drivers array — check whether v4 added a registration hook, since that’s often the cleanest upgrade path if you were on a custom v3 driver.How can I help you explore Laravel packages today?