PHPackages                             nevadskiy/laravel-geonames - 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. [Database &amp; ORM](/categories/database)
4. /
5. nevadskiy/laravel-geonames

ActiveLibrary[Database &amp; ORM](/categories/database)

nevadskiy/laravel-geonames
==========================

Populate your database using the GeoNames service.

0.7.0(1y ago)2715.1k—2%9[6 issues](https://github.com/xalaida/laravel-geonames/issues)[1 PRs](https://github.com/xalaida/laravel-geonames/pulls)MITPHPPHP ^7.3|^8.0CI passing

Since Mar 18Pushed 1y ago1 watchersCompare

[ Source](https://github.com/xalaida/laravel-geonames)[ Packagist](https://packagist.org/packages/nevadskiy/laravel-geonames)[ Docs](https://github.com/nevadskiy/laravel-geonames)[ RSS](/packages/nevadskiy-laravel-geonames/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (7)Versions (21)Used By (0)

🌎 Populate your database using the [GeoNames](https://www.geonames.org/) service
================================================================================

[](#-populate-your-database-using-the-geonames-service)

[![PHPUnit](https://camo.githubusercontent.com/e6064dad998d471f76fb912a18895485c11c90d43af48b52a91cebec87629fee/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6e65766164736b69792f6c61726176656c2d67656f6e616d65732f706870756e69742e796d6c3f6272616e63683d6d6173746572)](https://packagist.org/packages/nevadskiy/laravel-geonames)[![Code Coverage](https://camo.githubusercontent.com/59ff543de8b4d4ba0de34e21c46321469fede10493606cfa387300cb106d08c0/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636f762f632f6769746875622f6e65766164736b69792f6c61726176656c2d67656f6e616d65733f746f6b656e3d39583641515159435041)](https://packagist.org/packages/nevadskiy/laravel-geonames)[![Latest Stable Version](https://camo.githubusercontent.com/d8717661d3131b698541aa0cde8365ace990c9b0fc1ee499fb92b1349f10fde6/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6e65766164736b69792f6c61726176656c2d67656f6e616d6573)](https://packagist.org/packages/nevadskiy/laravel-geonames)[![License](https://camo.githubusercontent.com/81569d864e1a3daabc844dab4da9b19f86746d2e78348f7019e75d8b937709bc/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f6e65766164736b69792f6c61726176656c2d67656f6e616d6573)](https://packagist.org/packages/nevadskiy/laravel-geonames)

[![Stand With Ukraine](https://raw.githubusercontent.com/vshymanskyy/StandWithUkraine/main/banner-direct-single.svg)](https://stand-with-ukraine.pp.ua)

🗒️ Description
--------------

[](#️-description)

The package is useful for applications that rely on the geo data.

By default, it provides 4 models: `Continent`, `Country`, `Division`, `City` and translations for them.

The translations are powered by the [nevadskiy/laravel-translatable](https://github.com/nevadskiy/laravel-translatable) package.

The package also keeps the data **up-to-date** by fetching daily modifications provided by the [GeoNames](https://www.geonames.org/) service and uses them to synchronize your own database.

🔌 Installation
--------------

[](#-installation)

```
composer require nevadskiy/laravel-geonames
```

If you are going to use translations, you also need to install an additional package.

```
composer require nevadskiy/laravel-translatable
```

✅ Requirements
--------------

[](#-requirements)

- Laravel `8.0` or newer
- PHP `7.3` or newer

🔨 Usage
-------

[](#-usage)

Publish the package resources using the command:

```
php artisan vendor:publish --tag=geonames-migrations --tag=geonames-models
```

### Seeding

[](#seeding)

Before seeding, make sure you run the database migrations.

```
php artisan migrate
```

Then, run the seeding process.

```
php artisan geonames:seed
```

It will download geonames resources and insert the dataset into your database.

> Note that the seeding process may take some time. On average, it takes about 40 minutes (without downloading time) to seed the full dataset with translations.

> If you have issues with memory leaks during seeding, check out [this section](#memory-leaks).

### Schedule updates

[](#schedule-updates)

Add the following code to the `app/Console/Kernel.php` file if you want to receive geonames daily updates.

Geonames daily updates are published at 3:00 in the UTC time zone, so to be sure that they are already available, it is recommended to run the command a bit later.

```
protected function schedule(Schedule $schedule)
{
    $schedule->command('geonames:daily-update')->dailyAt('4:00');
}
```

> Note that time is specified for the `UTC` timezone.

### Syncing

[](#syncing)

If you missed some daily updates or just decided to change seeder filters, you can sync your database records according to the current geonames dataset.

```
php artisan geonames:sync
```

This command will create missing records, remove redundant ones, and updated modified ones according to the current dataset.

> Note that the `geoname_id` and `alternate_name_id` fields is required to synchronize data.

### Customization

[](#customization)

If you want to customize migrations or data that should be imported, you can simply do this by overriding the default seeders.

To do that, publish the package seeders using command:

```
php artisan vendor:publish --tag=geonames-seeders
```

Publish the package config:

```
php artisan vendor:publish --tag=geonames-config
```

Then, specify published seeders in the `config/geonames.php` file:

```
'seeders' => [
    Database\Seeders\Geo\ContinentSeeder::class,
    Database\Seeders\Geo\ContinentTranslationSeeder::class,
    Database\Seeders\Geo\CountrySeeder::class,
    Database\Seeders\Geo\CountryTranslationSeeder::class,
    Database\Seeders\Geo\DivisionSeeder::class,
    Database\Seeders\Geo\DivisionTranslationSeeder::class,
    Database\Seeders\Geo\CitySeeder::class,
    Database\Seeders\Geo\CityTranslationSeeder::class,
]
```

### Filtering

[](#filtering)

To reduce the database size, you can set up filters for seeding only those geo data that you really need in your application.

For example, you can set the minimum population for the city. All cities with smaller population will not be imported.

To do that, override the `$minPopulation` property in the `CitySeeder` class.

To have full control over this behaviour, override the `filter` method of the seeder.

#### Attributes mapping

[](#attributes-mapping)

To add custom fields to the table, you also need to tell the seeder how to fill those fields using the `mapAttributes` method.

The `mapAttributes` method should return all attributes of the database record, including timestamps, because model events will not be fired during seeding process since the package uses a bulk insert strategy. However, all model casts and mutators will be applied as usual.

For example, if you want to use UUIDs as primary keys, you can extend the original seeder as following:

```
