Installation:
composer require anvargear/laravel-states-col
For Laravel 5.5+, no manual app.php edits are needed. For older versions, add the provider and alias as shown in the README.
Publish Config (Optional):
php artisan vendor:publish --provider="AnvaGear\States\StatesServiceProvider"
This generates a migration and config file (default table name: Colstates).
Run Migration:
php artisan migrate
First Use Case: Fetch all departments or cities:
use StatesCol;
// Get all departments (states)
$departments = StatesCol::departments();
// Get all cities for a department (e.g., Bogotá's department code: 11)
$cities = StatesCol::cities(11);
Data Retrieval:
$departments = StatesCol::departments();
dane_code).
$cities = StatesCol::cities($departmentDaneCode);
dane_code.
$department = StatesCol::department($departmentDaneCode);
dane_code.
$city = StatesCol::city($cityDaneCode);
Integration with Eloquent Models:
Add a relationship to your model (e.g., User, Company):
public function department()
{
return $this->belongsTo(Department::class, 'department_id');
}
Use the facade to populate dropdowns or validate inputs:
$departmentOptions = StatesCol::departments()->pluck('name', 'dane_code');
Form Validation: Validate department/city codes or names:
use Illuminate\Support\Facades\Validator;
$validator = Validator::make($request->all(), [
'department_code' => 'required|exists:Colstates,dane_code',
'city_code' => 'required|exists:Colstates,dane_code',
]);
Caching:
Cache department/city data for performance (e.g., in AppServiceProvider):
public function boot()
{
Cache::remember('colombia_states', now()->addDays(30), function () {
return StatesCol::departments();
});
}
Custom Queries: Extend the facade or service provider to add custom queries (e.g., search by name):
// In a service class or facade extension
public static function searchDepartments($query)
{
return self::departments()->where('name', 'like', "%{$query}%");
}
API Responses: Return standardized responses for APIs:
return response()->json([
'data' => StatesCol::departments(),
'meta' => ['count' => StatesCol::departments()->count()]
]);
Localization: Use the data for localized content (e.g., region-specific features):
$currentDepartment = StatesCol::department($user->department_id);
return view('welcome', ['region' => $currentDepartment->name]);
Migration Conflicts:
Colstates) conflicts with existing tables, rename it via config after publishing:
// config/states.php
'table' => 'custom_col_states',
php artisan migrate:fresh if the table already exists.Data Accuracy:
dane_code) against official sources (e.g., DANE Colombia).dane_code is 11 (correct), but cross-check other regions.Facade vs. Service Container:
StatesCol facade for consistency:
// Bad: Direct instantiation
$service = app('states');
// Good: Facade
$departments = StatesCol::departments();
Laravel Version Compatibility:
dev-master and lacks Laravel version constraints. Test thoroughly in your Laravel version (e.g., 8/9/10). For older versions (<5.5), manual app.php edits are required.Check Published Files:
database/migrations/ and config/states.php for customizations.Query Logs:
DB::enableQueryLog();
$departments = StatesCol::departments();
dd(DB::getQueryLog());
Data Dumps:
dd(StatesCol::departments()->first()->toArray());
Custom Attributes: Add computed attributes to models (e.g., full department name with code):
// In Department model
public function getFullNameAttribute()
{
return "{$this->dane_code} - {$this->name}";
}
Service Provider Extensions:
Bind additional methods to the service container in StatesServiceProvider:
$this->app->bind('states', function () {
return new StatesManager(); // Custom class
});
Testing: Mock the facade in tests:
StatesCol::shouldReceive('departments')->once()->andReturn(collect([...]));
Seeding:
Seed the Colstates table manually if needed:
// database/seeds/ColstatesSeeder.php
public function run()
{
DB::table('Colstates')->insert([...]);
}
How can I help you explore Laravel packages today?