composer require shimadotdev/iran-regions and publish migrations with php artisan iran-regions:install.Iran facade:
use Shimadotdev\IranRegions\Iran;
// Get Tehran province
$tehran = Iran::province()->where('slug', 'tehran')->first();
// Get all cities in Tehran with eager-loaded relations
$tehranCities = Iran::city()->where('province_id', $tehran->id)->with('province')->get();
app/Models/Province.php and app/Models/City.php (auto-generated after installation).resources/lang/{fa,en}/iranRegions.php for localization.// Get all active cities in a province
$activeCities = Iran::city()
->where('province_id', $province->id)
->where('is_active', 1)
->with('province')
->get();
// Find cities within 50km of a point (pseudo-code)
$nearbyCities = Iran::city()
->whereBetween('latitude', [$lat - 0.5, $lat + 0.5])
->whereBetween('longitude', [$lng - 0.5, $lng + 0.5])
->get();
// Switch between Persian/English names dynamically
$name = app()->getLocale() === 'fa'
? trans("iranRegions::provinces.{$province->slug}")
: $province->name;
// In User.php
public function province()
{
return $this->belongsTo(Province::class, 'province_id');
}
use Shimadotdev\IranRegions\Rules\ValidProvince;
$request->validate([
'province' => ['required', new ValidProvince],
]);
return response()->json([
'user' => $user,
'location' => [
'province' => $user->province->slug,
'city' => $user->city?->slug,
],
]);
slug for queries (e.g., where('slug', 'tehran')), not name (case-sensitive and may change).iran-regions:install fails, manually run:
php artisan migrate
province_id/city_id foreign keys exist in your database.id conflicts with Laravel’s default).php artisan vendor:publish --tag=iran-regions-lang
slug structure in resources/lang/{fa,en}/iranRegions.php matches the database.Province/City models with scopes:
// In Province.php
public function scopeByCallingCode($query, $code)
{
return $query->where('calling_code', $code);
}
population):
// In DatabaseSeeder.php
Iran::city()->update(['population' => 1000000]); // Example
// routes/api.php
Route::get('/provinces/{slug}/cities', [CityController::class, 'index']);
$province = Province::factory()->create();
$city = City::factory()->for($province)->create();
```markdown
### **Pro Tips for Daily Use**
- **Eager Loading**: Always use `with()` to avoid N+1 queries when fetching relations:
```php
$users = User::with(['province.cities'])->get();
$provinces = Cache::remember('all-provinces', now()->addHours(1), function() {
return Iran::province()->get();
});
use Shimadotdev\IranRegions\Rules\ValidCity;
$request->validate([
'city' => ['required', new ValidCity],
]);
// In AppServiceProvider
function provinceName($slug, $locale = 'fa')
{
return trans("iranRegions::provinces.{$slug}");
}
How can I help you explore Laravel packages today?