PHP code example of hakanispirli / laravel-turkey-geo-database

1. Go to this page and download the library: Download hakanispirli/laravel-turkey-geo-database library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

hakanispirli / laravel-turkey-geo-database example snippets


use Webmarka\TurkeyGeo\Models\City;

$cities = City::all();

$ankara = City::where('name', 'ANKARA')->first();
$districts = $ankara->districts;

use Webmarka\TurkeyGeo\Models\District;

$district = District::find(1);
$neighborhoods = $district->neighborhoods;

use Webmarka\TurkeyGeo\Models\Neighborhood;

$neighborhoods = Neighborhood::where('postal_code', '06100')->get();

// Controller
public function create()
{
    $cities = City::orderBy('name')->get();
    return view('address.create', compact('cities'));
}

// Route
Route::get('/api/districts/{cityId}', function ($cityId) {
    return District::where('city_id', $cityId)
        ->orderBy('name')
        ->get(['id', 'name']);
});

$neighborhood = Neighborhood::with('district.city')->find($id);

echo $neighborhood->name; // Neighborhood name
echo $neighborhood->district->name; // District name
echo $neighborhood->district->city->name; // City name
echo $neighborhood->postal_code; // Postal code

// Load city with all its districts
$city = City::with('districts')->find(7);

// Load district with neighborhoods
$district = District::with('neighborhoods')->find(1);

use Illuminate\Validation\Rule;

public function rules()
{
    return [
        'city_id' => ')->where('city_id', $this->city_id),
        ],
        'neighborhood_id' => [
            '

use Illuminate\Support\Facades\Cache;

$cities = Cache::remember('turkish-cities', 3600, function () {
    return City::orderBy('name')->get();
});

// config/turkey-geo.php
'seeding' => [
    'batch_size' => 500, // Default is 1000
],
bash
# 1. Publish migrations (creates timestamped migration files)
php artisan vendor:publish --tag=turkey-geo-migrations

# 2. Publish data files
php artisan vendor:publish --tag=turkey-geo-data

# 3. Run migrations
php artisan migrate

# 4. Seed the database
php artisan db:seed --class="Webmarka\TurkeyGeo\Database\Seeders\TurkeyGeoSeeder"
bash
# 1. Publish migrations
php artisan vendor:publish --tag=turkey-geo-migrations

# 2. Customize the migration files in database/migrations/
# Add your custom columns, indexes, or modifications

# 3. Publish data files
php artisan vendor:publish --tag=turkey-geo-data

# 4. Run migrations
php artisan migrate

# 5. Seed the database
php artisan db:seed --class="Webmarka\TurkeyGeo\Database\Seeders\TurkeyGeoSeeder"
bash
php artisan vendor:publish --tag=turkey-geo-config
bash
composer dump-autoload