PHPackages                             dynamiccarrots/laravel-ip2location - 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. dynamiccarrots/laravel-ip2location

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

dynamiccarrots/laravel-ip2location
==================================

Use the free Ip2Location LITE database within Laravel

1.0.0(9y ago)123PHPPHP &gt;=5.3.0

Since Feb 23Pushed 9y agoCompare

[ Source](https://github.com/dynamiccarrots/Laravel-Ip2Location)[ Packagist](https://packagist.org/packages/dynamiccarrots/laravel-ip2location)[ RSS](/packages/dynamiccarrots-laravel-ip2location/feed)WikiDiscussions master Synced yesterday

READMEChangelog (5)Dependencies (1)Versions (6)Used By (0)

Laravel-Ip2Location
===================

[](#laravel-ip2location)

Ip2Location provides a free non-intrusive database to help you identify your visitors geographical location.

The data you receive from a users IP Address will depend on what database size you download from Ip2Location. The largest of these consists of Country, Region, City, Latitude, Longitude, Zipcode and Timezone.

Only download the database that you need, the large databases can have over 4 million rows. The smallest one has about 160K rows.

As this package uses the LITE/free version of Ip2Location you are not guaranteed 100% accuracy.

\#Installation

### Download Ip2Location Database

[](#download-ip2location-database)

Visit this page and download one of the DB files as a CSV, these range from DB1 to DB11.

You will need to create a folder in the database directory called "ip2location" and store the CSV in here with the name "ip2location.csv".

You should have the following structure.

```
    ├── database/
    │   ├── ip2location/
    │   │   ├── ip2location.csv

```

### Migrations

[](#migrations)

Create a new migration file in laravel by running the following command.

```
# php artisan make:migration create_ip2location_table

```

Copy the following migration in to the newly created migration file.

```
   /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('ip2location', function (Blueprint $table) {
          $table->integer('ip_from')->unsigned();
          $table->integer('ip_to')->unsigned();
          $table->char('country_code', 2);
          $table->string('country_name', 64)->nullable();
          $table->string('region_name', 128)->nullable();
          $table->string('city_name', 128)->nullable();
          $table->double('latitude')->nullable();
          $table->double('longitude')->nullable();
          $table->string('zip_code', 30)->nullable();
          $table->string('time_zone', 8)->nullable();
          // Setting index
          $table->index('ip_from', 'idx_ip_from');
          $table->index('ip_to', 'idx_ip_to');
          $table->index(['ip_from', 'ip_to'], 'idx_ip_from_to');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('ip2location');
    }
```

Run the migration.

```
# php artisan migrate

```

### Create update command

[](#create-update-command)

Due to the fact IP address ranges get moved around from ISP to ISP all the time. We need to update our database on a monthly basis if you want to stay up to date.

We need to have a command that will allow us to change the ip2location.csv file and use its data to refresh our database.

Run the following command.

```
# php artisan make:command RefreshIp2Location

```

This will create a new file in app/Console/Commands called RefreshIp2Location.php.

Replace the code in this new file with this.

```
