PHPackages                             dnkmdg/local-geoip-for-laravel - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. dnkmdg/local-geoip-for-laravel

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

dnkmdg/local-geoip-for-laravel
==============================

Local IP geolocation for Laravel using MaxMind MMDB

v0.0.9(3mo ago)1122MITPHPPHP ^8.1

Since Feb 16Pushed 3mo agoCompare

[ Source](https://github.com/dnkmdg/local-geoip-for-laravel)[ Packagist](https://packagist.org/packages/dnkmdg/local-geoip-for-laravel)[ RSS](/packages/dnkmdg-local-geoip-for-laravel/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (4)Dependencies (9)Versions (9)Used By (0)

dnkmdg/local-geoip-for-laravel
==============================

[](#dnkmdglocal-geoip-for-laravel)

Local IP geolocation for Laravel using MaxMind MMDB.

Features
--------

[](#features)

- Local MMDB lookups via `geoip2/geoip2`
- No runtime external HTTP calls during request handling
- Swappable abstraction via `Dnkmdg\LocalGeoIp\Contracts\GeoIpLookup`
- Normalized DTO: `Dnkmdg\LocalGeoIp\Data\GeoIpLocationData`
- Trusted-proxy-aware request IP candidate resolver
- Built-in MMDB update command: `geoip:update-mmdb`

Install
-------

[](#install)

```
composer require dnkmdg/local-geoip-for-laravel geoip2/geoip2
```

Create a MaxMind GeoLite account and license key before first database download:

- Signup:
- License keys:
- Update docs:

Guided install (with first download prompt)
-------------------------------------------

[](#guided-install-with-first-download-prompt)

```
php artisan geoip:install
```

This command publishes config and, in interactive mode, prompts whether to run the first MMDB download immediately.

Config keys
-----------

[](#config-keys)

- `database_path` (default: `storage/app/geoip/GeoLite2-Country.mmdb`)
- `cache_ttl` (seconds, default: `86400`)
- `database_max_age_days` (default: `45`)
- `trusted_proxies` (array from env CSV)
- `forwarded_headers` (default: `CF-Connecting-IP,True-Client-IP,X-Forwarded-For,X-Real-IP`)
- `override_secret` (optional)
- `update.enabled` (default: `true`; scheduler policy toggle)
- `update.account_id` (`MAXMIND_ACCOUNT_ID`)
- `update.license_key` (`MAXMIND_LICENSE_KEY`)
- `update.edition_id` (`LOCAL_GEOIP_UPDATE_EDITION_ID`, default: `GeoLite2-Country`)
- `update.download_url` (`LOCAL_GEOIP_UPDATE_DOWNLOAD_URL`, default: `https://download.maxmind.com/geoip/databases/{edition_id}/download`)

Usage
-----

[](#usage)

```
use Dnkmdg\LocalGeoIp\Contracts\GeoIpLookup;
use Dnkmdg\LocalGeoIp\RequestIpResolver;

$candidates = app(RequestIpResolver::class)->resolveCandidates($request);
$geoIp = app(GeoIpLookup::class);

$location = null;
foreach ($candidates as $candidate) {
    $location = $geoIp->lookup($candidate);
    if ($location?->countryCode !== null) {
        break;
    }
}
```

Facade usage:

```
use Dnkmdg\LocalGeoIp\Facades\GeoIpLookup;

$location = GeoIpLookup::resolve('8.8.8.8');
```

Consumer testing
----------------

[](#consumer-testing)

Example: fake contract responses in a feature test

```
