Country - State - City

Which is the best package to populate some dropdown in forms? Squire doesn't have cities...
11 Replies
toeknee
toeknee3mo ago
GitHub
GitHub - khsing/laravel-world: provide countries, states, and citie...
provide countries, states, and cities relations and database. - khsing/laravel-world
Soundmit
Soundmit3mo ago
thanks, isn't heavy on the db?
toeknee
toeknee3mo ago
Not really, it's DB but how often are you truly calling the country/state/city?
Soundmit
Soundmit3mo ago
not very often
toeknee
toeknee3mo ago
So it's not an issue, theres very little data in the grand schema of it.
Soundmit
Soundmit3mo ago
i'm in trouble in implement world

Forms\Components\Select::make('country')
->native(false)
->searchable()
->live()
->options(World::Countries()->pluck('name', 'code'))
->afterStateUpdated(function ($state, $set) {
$set('province_code', $state);
}),
Forms\Components\Select::make('province')
->label('Provincia')
->native(false)
->options(function (callable $get) {
if ($country = $get('province_code')) {
return World::getByCode($country)->children()->pluck('name', 'id');
}
})
->searchable(),


Forms\Components\Select::make('country')
->native(false)
->searchable()
->live()
->options(World::Countries()->pluck('name', 'code'))
->afterStateUpdated(function ($state, $set) {
$set('province_code', $state);
}),
Forms\Components\Select::make('province')
->label('Provincia')
->native(false)
->options(function (callable $get) {
if ($country = $get('province_code')) {
return World::getByCode($country)->children()->pluck('name', 'id');
}
})
->searchable(),

in the country select i saw the name of the country but in the db code is saved instead of id in the city field i saw the cities name in the select but when i save, in the db i have the id and in the frontend i saw the id and not the name
toeknee
toeknee3mo ago
That is normal. You get the name by id then
Soundmit
Soundmit3mo ago
ok but in the db i need to save country id and city id then in the select i need to see the name anyone can help?
toeknee
toeknee3mo ago
What with? You said you need to save the country id and the city id? What's the problem?
Soundmit
Soundmit3mo ago
I would like to have the IDs in the database instead of the codes. Is it still okay if I save the codes? It is currently working. However, what I don't have is this: In an info list, I display the country like this:
Components\TextEntry::make('country')
->formatStateUsing(function ($state) {
return World::getByCode($state)->name;
}),
Components\TextEntry::make('country')
->formatStateUsing(function ($state) {
return World::getByCode($state)->name;
}),
But I don't have a Laravel-World function that returns the name of the province using the code saved in the database, something like this:
Components\TextEntry::make('province')
->formatStateUsing(function ($state) {
return World::getByCode($state)->name;
}),
Components\TextEntry::make('province')
->formatStateUsing(function ($state) {
return World::getByCode($state)->name;
}),
toeknee
toeknee3mo ago
Do you know how php,laravel and databases work fully or are you just coming to it with filamentphp? What you should do is on the model for where you implemented the world. Is to add a relationship for country and province, then when they render anywhere they render the name with :
Components\TextEntry::make('province.name')
Components\TextEntry::make('province.name')