usamamuneerchaudhary / country-city-state
Country City State Data Provider for Laravel 12+
Installs: 2 506
Dependents: 0
Suggesters: 0
Security: 0
Stars: 24
Watchers: 1
Forks: 1
Open Issues: 0
Requires
- php: ^8.3|^8.4
- illuminate/database: ^12.0
- illuminate/support: ^12.0
Requires (Dev)
- phpunit/phpunit: ^12.0
README
World's Country City State Data for Laravel ^12.0
with PHP ^8.3|^8.4
and phone codes support as per ISO standards.
๐ Requirements
- PHP: ^8.3|^8.4
- Laravel: ^12.0
๐ฆ Installation
You can install the package via composer:
composer require usamamuneerchaudhary/country-city-state
๐ง Setup
1. Publish Assets
# Publish migrations, models, helpers, and seeders php artisan vendor:publish --tag=CountryCityState # Publish configuration file php artisan vendor:publish --tag=config
2. Run Migrations
php artisan migrate
3. (Optional) Run Seeder
php artisan db:seed --class=UsamaMuneerChaudhary\\CountryCityState\\seeds\\CountryCityStateTableSeeder
๐ก Usage
Using the Facade
use UsamaMuneerChaudhary\CountryCityState\Facades\CountryCityState; // Get all active countries $countries = CountryCityState::getCountries(); // Get states by country ID $states = CountryCityState::getStatesByCountry(1); // Get cities by state ID $cities = CountryCityState::getCitiesByState(1); // Get phone codes by country ID $phoneCodes = CountryCityState::getPhoneCodesByCountry(1); // Find country by ISO code $country = CountryCityState::findCountryByIso('US');
Using the Service Container
$service = app('country-city-state'); $countries = $service->getCountries();
Direct Model Usage
use UsamaMuneerChaudhary\CountryCityState\Models\Country; use UsamaMuneerChaudhary\CountryCityState\Models\State; use UsamaMuneerChaudhary\CountryCityState\Models\City; // Find country by ISO code $country = Country::where('iso_code2', 'US')->first(); // Get all states for a country $states = $country->states; // Get all cities for a state $cities = $states->first()->cities; // Find state by name $state = State::where('name', 'California')->first(); // Find city by name $city = City::where('name', 'Los Angeles')->first();
Working with Relationships
// Country -> States -> Cities $country = Country::find(1); $states = $country->states; // HasMany relationship $state = $states->first(); $cities = $state->cities; // HasMany relationship // Reverse relationships $city = City::find(1); $state = $city->state; // BelongsTo relationship $country = $state->country; // BelongsTo relationship
โ๏ธ Configuration
The package configuration file will be published to config/country-city-state.php
. You can customize:
- Default Status: Set default status for new records (
active
/inactive
) - Table Names: Customize database table names if needed
- Cache Settings: Enable/disable caching and set TTL
- Seeder Behavior: Control seeder truncation behavior
return [ 'default_status' => 'active', 'tables' => [ 'countries' => 'countries', 'states' => 'states', 'cities' => 'cities', 'country_phone_codes' => 'country_phone_codes', ], 'cache' => [ 'enabled' => true, 'ttl' => 3600, // 1 hour ], 'seeder' => [ 'truncate_before_seeding' => false, ], ];
๐๏ธ Database Structure
The package creates the following tables:
Countries Table
id
- Primary keyname
- Country nameiso_code2
- 2-letter ISO code (e.g., US, GB)iso_code3
- 3-letter ISO code (e.g., USA, GBR)num_code
- Numeric ISO codestatus
- Active/Inactive statuscreated_at
,updated_at
- Timestampsdeleted_at
- Soft delete timestamp
States Table
id
- Primary keycountry_id
- Foreign key to countries tablename
- State/Province namestatus
- Active/Inactive statuscreated_at
,updated_at
- Timestampsdeleted_at
- Soft delete timestamp
Cities Table
id
- Primary keystate_id
- Foreign key to states tablename
- City namestatus
- Active/Inactive statuscreated_at
,updated_at
- Timestampsdeleted_at
- Soft delete timestamp
Country Phone Codes Table
id
- Primary keyphone_code
- Country calling codeintl_dialing_prefix
- International dialing prefixcountry_id
- Foreign key to countries tablecreated_at
,updated_at
- Timestamps
๐งช Testing
composer test
๐ What's New in v3.0
- โ PHP 8.3+ Support - Modern PHP features and performance
- โ Laravel 12+ Support - Latest Laravel framework compatibility
- โ Modern Migration Syntax - Anonymous classes and improved foreign keys
- โ Enhanced Service Class - Useful methods for common operations
- โ Proper Type Hints - Better IDE support and code quality
- โ Configuration Support - Customizable package settings
- โ Improved Namespaces - Better package organization
- โ PHPUnit 11 - Latest testing framework
๐ Upgrading from v1.x
If you're upgrading from version 1.x, please note:
- PHP 8.3+ Required - Update your PHP version
- Laravel 12+ Required - Update your Laravel version
- Model Namespaces Changed - Update any direct model references
- Migration Updates - May need to rollback and re-run migrations
See UPGRADE_GUIDE.md for detailed upgrade instructions.
๐ค Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email hello@usamamuneer.me instead of using the issue tracker.
๐ License
The MIT License (MIT). Please see License File for more information.
๐จโ๐ป Credits
Note: This package is now fully compatible with PHP 8.3+ and Laravel 12+. For older versions, please use v1.x of the package.