├── .gitignore ├── src ├── Data │ └── pk_20220810.xlsx ├── Database │ ├── Seeders │ │ └── CitiesSeeder.php │ └── Migrations │ │ ├── 2017_04_16_152547_create_cities_table.php │ │ ├── 2017_04_16_152754_create_counties_table.php │ │ ├── 2017_04_16_152755_create_districts_table.php │ │ └── 2017_04_16_152756_create_neighborhoods_table.php ├── Console │ └── CitiesCommand.php ├── Models │ ├── City.php │ ├── Neighborhood.php │ ├── County.php │ └── District.php ├── Config │ └── config.php ├── Providers │ └── TurkeyCitiesServiceProvider.php └── Imports │ └── DataImport.php ├── composer.json ├── LICENSE └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | composer.lock 2 | vendor 3 | .idea -------------------------------------------------------------------------------- /src/Data/pk_20220810.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cuneytyuksel/turkey-cities/HEAD/src/Data/pk_20220810.xlsx -------------------------------------------------------------------------------- /src/Database/Seeders/CitiesSeeder.php: -------------------------------------------------------------------------------- 1 | run(); 26 | $this->line('Seeder completed.'); 27 | } 28 | } -------------------------------------------------------------------------------- /src/Database/Migrations/2017_04_16_152547_create_cities_table.php: -------------------------------------------------------------------------------- 1 | increments('id'); 18 | $table->string('name'); 19 | }); 20 | } 21 | 22 | /** 23 | * Reverse the migrations. 24 | * 25 | * @return void 26 | */ 27 | public function down() 28 | { 29 | Schema::dropIfExists('cities'); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "cuneytyuksel/turkey-cities", 3 | "description": "Turkey Cities and States - Türkiye İl ve İlçeler (PTT)", 4 | "license": "MIT", 5 | "keywords": [ 6 | "laravel", 7 | "turkey", 8 | "city", 9 | "state", 10 | "ptt", 11 | "turkiye", 12 | "il", 13 | "ilçe" 14 | ], 15 | "authors": [ 16 | { 17 | "name": "Cüneyt Yüksel", 18 | "email": "iletisim@cuneytyuksel.net" 19 | } 20 | ], 21 | "require": { 22 | "maatwebsite/excel": "^3.1" 23 | }, 24 | "autoload": { 25 | "psr-4": { 26 | "Turkey\\Cities\\": "src/" 27 | } 28 | }, 29 | "extra": { 30 | "laravel": { 31 | "providers": [ 32 | "Turkey\\Cities\\Providers\\TurkeyCitiesServiceProvider" 33 | ] 34 | } 35 | }, 36 | "minimum-stability": "dev", 37 | "prefer-stable": true 38 | } -------------------------------------------------------------------------------- /src/Models/City.php: -------------------------------------------------------------------------------- 1 | table = config('turkey-cities.tables.city', 'cities'); 32 | } 33 | 34 | public function counties() 35 | { 36 | return $this->hasMany(config('turkey-cities.models.county', 'Turkey\Cities\Models\County')); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/Config/config.php: -------------------------------------------------------------------------------- 1 | __DIR__ . '/../Data/pk_20220810.xlsx', // set your default PTT cities xlsx file. 11 | 12 | 'models' => [ 13 | 'city' => 'Turkey\Cities\Models\City', 14 | 'county' => 'Turkey\Cities\Models\County', 15 | 'district' => 'Turkey\Cities\Models\District', 16 | 'neighborhood' => 'Turkey\Cities\Models\Neighborhood' 17 | ], 18 | 19 | //Database table name for models 20 | 'tables' => [ 21 | 'city' => 'cities', 22 | 'county' => 'counties', 23 | 'district' => 'districts', 24 | 'neighborhood' => 'neighborhoods' 25 | ], 26 | 27 | //Import settings 28 | 'import' => [ 29 | 'county' => true, 30 | 'district' => true, 31 | 'neighborhood' => true 32 | ] 33 | ]; 34 | -------------------------------------------------------------------------------- /src/Models/Neighborhood.php: -------------------------------------------------------------------------------- 1 | table = config('turkey-cities.tables.neighborhoods', 'neighborhoods'); 32 | } 33 | 34 | public function district() 35 | { 36 | return $this->belongsTo(config('turkey-cities.models.district', 'Turkey\Cities\Models\District')); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/Database/Migrations/2017_04_16_152754_create_counties_table.php: -------------------------------------------------------------------------------- 1 | increments('id'); 18 | $table->integer('city_id')->unsigned(); 19 | $table->string('name'); 20 | 21 | $table->foreign('city_id')->references('id')->on(config('turkey-cities.tables.city', 'cities'))->onDelete('cascade')->onUpdate('cascade'); 22 | }); 23 | } 24 | 25 | /** 26 | * Reverse the migrations. 27 | * 28 | * @return void 29 | */ 30 | public function down() 31 | { 32 | Schema::dropIfExists(config('turkey-cities.tables.county', 'counties')); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/Database/Migrations/2017_04_16_152755_create_districts_table.php: -------------------------------------------------------------------------------- 1 | increments('id'); 18 | $table->integer('county_id')->unsigned(); 19 | $table->string('name'); 20 | 21 | $table->foreign('county_id')->references('id')->on(config('turkey-cities.tables.county', 'counties'))->onDelete('cascade')->onUpdate('cascade'); 22 | }); 23 | } 24 | 25 | /** 26 | * Reverse the migrations. 27 | * 28 | * @return void 29 | */ 30 | public function down() 31 | { 32 | Schema::dropIfExists(config('turkey-cities.tables.district', 'districts')); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/Models/County.php: -------------------------------------------------------------------------------- 1 | table = config('turkey-cities.tables.county', 'counties'); 32 | } 33 | 34 | public function districts() 35 | { 36 | return $this->hasMany(config('turkey-cities.models.district', 'Turkey\Cities\Models\District')); 37 | } 38 | 39 | public function city() 40 | { 41 | return $this->belongsTo(config('turkey-cities.models.city', 'Turkey\Cities\Models\City')); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/Models/District.php: -------------------------------------------------------------------------------- 1 | table = config('turkey-cities.tables.districts', 'districts'); 32 | } 33 | 34 | public function neighborhoods() 35 | { 36 | return $this->hasMany(config('turkey-cities.models.neighborhood', 'Turkey\Cities\Models\Neighborhood')); 37 | } 38 | 39 | public function county() 40 | { 41 | return $this->belongsTo(config('turkey-cities.models.county', 'Turkey\Cities\Models\County')); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Cüneyt Yüksel 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /src/Database/Migrations/2017_04_16_152756_create_neighborhoods_table.php: -------------------------------------------------------------------------------- 1 | increments('id'); 18 | $table->integer('district_id')->unsigned(); 19 | $table->string('name'); 20 | $table->string('pk'); 21 | 22 | $table->foreign('district_id')->references('id')->on(config('turkey-cities.tables.district', 'districts'))->onDelete('cascade')->onUpdate('cascade'); 23 | }); 24 | } 25 | 26 | /** 27 | * Reverse the migrations. 28 | * 29 | * @return void 30 | */ 31 | public function down() 32 | { 33 | Schema::dropIfExists(config('turkey-cities.tables.neighborhood', 'neighborhoods')); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Türkiye İl, ilçe, semt, mahalle ve posta kodu 2 | PTT tarafından sunulan il, ilçe ve mahalle bilgilerinin veritabanı modeli ve bilgilerini laravel paketi olarak ekleyip veritabanına kolayca uygulayabilirsiniz. 3 | 4 | ## Install 5 | config/app.php içerisine service provider sınıfısımızı ekliyoruz. 6 | 7 | ``` 8 | composer require cuneytyuksel/turkey-cities 9 | ``` 10 | 11 | ## Service Provider 12 | config/app.php içerisine service provider sınıfısımızı ekliyoruz. 13 | 14 | ```php 15 | /* 16 | * Turkey Cities 17 | */ 18 | Turkey\Cities\Providers\TurkeyCitiesServiceProvider::class, 19 | ``` 20 | 21 | ## Vendor Publish 22 | Config dosyasını publish ederek gerekli değişikleri sağlayabilirsiniz. config/turkey-cities.php 23 | 24 | ``` 25 | php artisan vendor:publish --provider="Turkey\Cities\Providers\TurkeyCitiesServiceProvider" 26 | ``` 27 | 28 | ## Migration 29 | Veritabanında tabloları oluşturması için migration işlemi uyguluyoruz. 30 | 31 | ``` 32 | php artisan migrate 33 | ``` 34 | 35 | ## Data Entegrasyonu 36 | PTT tarafından verilen .xlsx uzantılı doyasımız dahili olarak paket içerisine bulunmaktadır fakat isteğiniz doğrultusunda turkey-cities.php config dosyasından yolunu değişitrip düzenlemiş olduğunuz dosya yolunu gösterebilirsiniz. (https://postakodu.ptt.gov.tr/) 37 | ``` 38 | php artisan turkey:cities 39 | ``` 40 | 41 | -------------------------------------------------------------------------------- /src/Providers/TurkeyCitiesServiceProvider.php: -------------------------------------------------------------------------------- 1 | registerConfig(); 29 | $this->loadMigrationsFrom(__DIR__ . '/../Database/Migrations'); 30 | 31 | $this->app->register('Maatwebsite\Excel\ExcelServiceProvider'); 32 | $loader = \Illuminate\Foundation\AliasLoader::getInstance(); 33 | $loader->alias('Excel', 'Maatwebsite\Excel\Facades\Excel'); 34 | 35 | $this->commands('command.turkey.cities'); 36 | $this->app->singleton('command.turkey.cities', function ($app) { 37 | return new \Turkey\Cities\Console\CitiesCommand(); 38 | }); 39 | } 40 | 41 | /** 42 | * Register config. 43 | * 44 | * @return void 45 | */ 46 | protected function registerConfig() 47 | { 48 | $this->publishes([ 49 | __DIR__ . '/../Config/config.php' => config_path('turkey-cities.php'), 50 | ]); 51 | 52 | $this->mergeConfigFrom( 53 | __DIR__ . '/../Config/config.php', 'turkey-cities' 54 | ); 55 | } 56 | 57 | /** 58 | * Get the services provided by the provider. 59 | * 60 | * @return array 61 | */ 62 | public function provides() 63 | { 64 | return array('command.turkey.cities'); 65 | } 66 | } -------------------------------------------------------------------------------- /src/Imports/DataImport.php: -------------------------------------------------------------------------------- 1 | $row[3], 'pk' => $row[4]]; 21 | } 22 | 23 | if (!empty($data) && count($data)) { 24 | foreach ($data as $cityName => $counties) { 25 | $cityName = trim($cityName); 26 | $city = $cityModel::firstOrNew(array('name' => $cityName)); 27 | $city->name = $cityName; 28 | $city->save(); 29 | 30 | if (config('turkey-cities.import.county')) { 31 | foreach ($counties as $countyName => $districts) { 32 | $countyName = trim($countyName); 33 | $county = $countyModel::firstOrNew(array('name' => $countyName, 'city_id' => $city->id)); 34 | $county->name = $countyName; 35 | $county->city_id = $city->id; 36 | $county->save(); 37 | 38 | if (config('turkey-cities.import.district')) { 39 | foreach ($districts as $districtName => $neighborhoods) { 40 | $districtName = trim($districtName); 41 | $district = $districtModel::firstOrNew(array('name' => $districtName, 'county_id' => $county->id)); 42 | $district->name = $districtName; 43 | $district->county_id = $county->id; 44 | $district->save(); 45 | 46 | if (config('turkey-cities.import.neighborhood')) { 47 | foreach ($neighborhoods as $neighborhoodData) { 48 | $neighborhoodName = trim($neighborhoodData['name']); 49 | $neighborhoodPk = trim($neighborhoodData['pk']); 50 | $neighborhood = $neighborhoodModel::firstOrNew(array('name' => $neighborhoodName, 'pk' => $neighborhoodPk, 'district_id' => $district->id)); 51 | $neighborhood->name = $neighborhoodName; 52 | $neighborhood->pk = $neighborhoodPk; 53 | $neighborhood->district_id = $district->id; 54 | $neighborhood->save(); 55 | } 56 | } 57 | } 58 | } 59 | } 60 | } 61 | } 62 | } 63 | } 64 | 65 | /** 66 | * @return int 67 | */ 68 | public function startRow(): int 69 | { 70 | return 2; 71 | } 72 | } 73 | --------------------------------------------------------------------------------