A Laravel package for importing and managing Italian geographical data from ISTAT.
- ๐ฎ๐น Import Italian regions, provinces, and municipalities from ISTAT
- ๐ Daily CSV caching to avoid unnecessary requests
- ๐ Eloquent models with hierarchical relationships
- โก Artisan command for easy data import
- ๐ง Fully configurable via configuration file
- ๐ UUID primary keys and soft deletes support
- ๐งช Comprehensive test suite with mocked HTTP requests
- PHP 8.3+
- Laravel 11.0+ or 12.0+
- league/csv 9.0+
- guzzlehttp/guzzle 7.0+
composer require plin-code/laravel-istat-geography
- Install the package:
composer require plin-code/laravel-istat-geography
- Publish the configuration:
php artisan vendor:publish --provider="PlinCode\IstatGeography\IstatGeographyServiceProvider"
- Run migrations:
php artisan migrate
- Import the data:
php artisan geography:import
That's it! You now have all Italian geographical data in your database.
Publish the configuration file:
php artisan vendor:publish --provider="PlinCode\IstatGeography\IstatGeographyServiceProvider"
Run the migrations to create the necessary tables:
php artisan migrate
To import all geographical data from ISTAT:
php artisan geography:import
The package provides three Eloquent models:
use PlinCode\IstatGeography\Models\Geography\Region;
$region = Region::where('name', 'Piemonte')->first();
$provinces = $region->provinces;
use PlinCode\IstatGeography\Models\Geography\Province;
$province = Province::where('code', 'TO')->first();
$municipalities = $province->municipalities;
$region = $province->region;
use PlinCode\IstatGeography\Models\Geography\Municipality;
$municipality = Municipality::where('name', 'Torino')->first();
$province = $municipality->province;
If you want to use the package models in your main project, you can extend them:
// app/Models/Region.php
namespace App\Models;
use PlinCode\IstatGeography\Models\Geography\Region as BaseRegion;
class Region extends BaseRegion
{
// Add your project-specific logic here
public function customMethod()
{
return $this->provinces()->count();
}
}
// app/Models/Province.php
namespace App\Models;
use PlinCode\IstatGeography\Models\Geography\Province as BaseProvince;
class Province extends BaseProvince
{
// Add your project-specific logic here
}
// app/Models/Municipality.php
namespace App\Models;
use PlinCode\IstatGeography\Models\Geography\Municipality as BaseMunicipality;
class Municipality extends BaseMunicipality
{
// Add your project-specific logic here
}
If you already have a geography:import
command in your project, you can replace it with the package's command:
// In app/Console/Kernel.php or in your existing command
Artisan::command('geography:import', function () {
$this->info('Starting geographical data import...');
try {
$count = \PlinCode\IstatGeography\Facades\IstatGeography::import();
$this->info("Import completed successfully! Imported {$count} municipalities.");
} catch (\Exception $e) {
$this->error('Error during import: ' . $e->getMessage());
}
})->purpose('Import regions, provinces and municipalities from ISTAT');
The config/istat-geography.php
file allows you to customize:
- Table names: Customize the database table names
- Model classes: Use your own model classes by extending the base ones
- CSV URL: Change the ISTAT data source URL
- Temporary file name: Customize the cache file name
return [
'tables' => [
'regions' => 'my_regions',
'provinces' => 'my_provinces',
'municipalities' => 'my_municipalities',
],
'models' => [
'region' => \App\Models\Region::class,
'province' => \App\Models\Province::class,
'municipality' => \App\Models\Municipality::class,
],
'import' => [
'csv_url' => 'https://custom-url.com/data.csv',
'temp_filename' => 'my_istat_data.csv',
],
];
id
(UUID, primary key)name
(string)istat_code
(string, unique)created_at
,updated_at
,deleted_at
id
(UUID, primary key)region_id
(UUID, foreign key)name
(string)code
(string, unique)istat_code
(string, unique)created_at
,updated_at
,deleted_at
id
(UUID, primary key)province_id
(UUID, foreign key)name
(string)istat_code
(string, unique)created_at
,updated_at
,deleted_at
Region
โhasMany
โProvince
Province
โbelongsTo
โRegion
Province
โhasMany
โMunicipality
Municipality
โbelongsTo
โProvince
Run the test suite:
composer test
The package includes:
- โ Unit tests for models and relationships
- โ Feature tests for the import service
- โ Mocked HTTP requests (no external dependencies)
- โ PHPStan static analysis
- โ Pest PHP testing framework
- Models and their relationships
- Import service with CSV processing
- Artisan command functionality
- Configuration handling
- Fork the project
- Create a feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add some amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
The MIT License (MIT). Please see License File for more information.