Skip to content
Service provider and DB downloader for Maxminds PHP API GeoIP2.
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.
src add checks for config values and DB file presence Nov 24, 2016
.gitignore move config dir Nov 21, 2016
LICENSE Update composer to include Laravel 6 Sep 12, 2019
composer.json change testbench composer require-dev to support cases of Laravel 5 Sep 12, 2019
phpunit.xml Update composer to include Laravel 6 Sep 12, 2019


Service provider and DB downloader, Laravel 5 & 6 for Maxminds PHP API GeoIP2.

License: MIT Build Status


In composer.json

"require": {
        "danielme85/laravel-geoip2": "dev-master",

or command: composer require danielme85/laravel-geoip2

Laravel 5.x

Add to your config/app.php under Service Providers
(If you use Laravel 5.5+ you could skip this step as Autodiscovery has been enabled for this package.)

//Service Providers
'Reader'  => danielme85\Geoip2\Facade\Reader::class,

Lumen 5.x

Add to your boostrap/app.php file

class_alias('danielme85\Geoip2\Facade\Reader, 'Reader');


Publish the config file to your Laravel projects

php artisan vendor:publish --provider="danielme85\Geoip2\Geoip2ServiceProvider"

The following default settings will work right away:

return [
    'geoip2' => [
        'downloadUrl' => '', //url db file download
        'tempFile' => 'app/GeoLite2-City.mmdb.gz', //temp download file name
        'dbName' => 'app/GeoLite2-City.mmdb', //Geoip DB filename
        'localhost' => '' //when running on localhost (or for general testing) you can specify a fake ip address here.


You need to download the Maxmind Geoip first, the default config is for the city version (about 30MB download, 50MB extracted).

php artisan geoip:download

With the DB file downloaded you are ready to get some location data:

use danielme85\Geoip2\Facade\Reader;
$reader = Reader::connect();
$result = $reader->city($ip);

Usage once you have the Reader:connect object is the same as maxminds documentation

Example usage, return json location data based on ipv4 address.

use danielme85\Geoip2\Facade\Reader;

function getLocation(Request $request) {
   $reader = Reader::connect();
   I was experiencing inaccurate results... until I remembered that my web server traffic was routed trough CloudFlare :p
   In that case CloudFlare provides the original client ip in the following header information.
   if (isset($_SERVER["HTTP_CF_CONNECTING_IP"])) {
   else {
       $ip = $request->ip();
   //the city() function from the GeoIp2 Php API will throw an exception if the ip-address is not found in the DB.
   try {
       $geodata = $reader->city($ip)->jsonSerialize(); //jsonSerialize seems to actually return an associative array.
   catch (\Exception $e) {
       return response()->json("Geo-location not found!", 500);

   return response()->json($geodata);

This product includes GeoLite2 data created by MaxMind, available from

You can’t perform that action at this time.