Installation:
composer require adrianmejias/laravel-states:~1.0
Add to config/app.php:
'providers' => [
AdrianMejias\States\StatesServiceProvider::class,
],
'aliases' => [
'States' => AdrianMejias\States\StatesFacade::class,
]
Set Up Database:
php artisan vendor:publish --provider="AdrianMejias\States\StatesServiceProvider"
php artisan states:migration
composer dump-autoload
Run migrations and seed:
php artisan migrate --seed
First Use Case: Fetch a state by abbreviation:
$state = States::getByAbbreviation('CA'); // Returns California data
Or fetch all states:
$allStates = States::all();
Retrieving State Data:
$state = States::getByAbbreviation('NY'); // Returns state object
$state = States::getByName('Texas');
$states = States::all(); // Returns collection of state objects
Integration with Models:
Add a relationship in your model (e.g., User):
public function state()
{
return $this->belongsTo(State::class);
}
Use in queries:
$usersInCalifornia = User::whereHas('state', function($query) {
$query->where('abbreviation', 'CA');
})->get();
Form Validation: Validate state abbreviations in Form Requests:
public function rules()
{
return [
'state' => 'required|state_abbreviation',
];
}
(Requires adding the validation rule to AppServiceProvider or a custom rule.)
Localization: Use state names in views:
{{ States::getByAbbreviation($user->state)->name }}
$states = States::where('region', 'West')->get();
$states = States::where('name', 'like', request('search').'%')->get();
return response()->json(States::all()->toArray());
Laravel 5 Only:
config/app.php structure, vendor:publish behavior).Database Dependencies:
states table in your database. Skipping migrations/seeding will break functionality.Validation Rule:
state_abbreviation validation rule is not included by default. You must manually register it in AppServiceProvider:
Validator::extend('state_abbreviation', function ($attribute, $value, $parameters, $validator) {
return States::getByAbbreviation($value) !== null;
});
Archived Package:
spatie/laravel-states for Laravel 6+.Missing States?:
Verify the states table exists and is populated. Check database/seeds/StatesSeeder.php for errors.
php artisan db:seed --class=StatesSeeder
Facade Not Found:
Ensure the States alias is correctly registered in config/app.php and the service provider is loaded.
Performance: Cache state collections if frequently accessed:
$states = Cache::remember('all_states', now()->addHours(1), function() {
return States::all();
});
Add Custom Fields:
Extend the states table migration to include additional columns (e.g., timezone, population):
Schema::table('states', function (Blueprint $table) {
$table->string('timezone')->nullable();
});
Update the seeder to populate these fields.
Support Non-US States:
The package supports other countries via the country_code column. Add a new seeder or migration for additional regions.
Custom Query Builder:
Extend the State model to add scopes:
public function scopeByRegion($query, $region)
{
return $query->where('region', $region);
}
Usage:
$westernStates = State::byRegion('West')->get();
How can I help you explore Laravel packages today?