F
Filamentβ€’5mo ago
ronssij

How to populate options from custom search results

So I was able to manipulate the select search using Gooogle places API.
class GooglePlaceField extends Select
{
protected string | Closure | null $country;

protected function setUp(): void
{
parent::setup();

$this->getSearchResultsUsing(static function (Select $component, ?string $search) {
$places = Places::autocomplete($search, ['types' => 'address']);

return collect($places)
->mapWithKeys(function ($place) {
return [$place->placeId() => $place->description()];
})->toArray();
});
}
}
class GooglePlaceField extends Select
{
protected string | Closure | null $country;

protected function setUp(): void
{
parent::setup();

$this->getSearchResultsUsing(static function (Select $component, ?string $search) {
$places = Places::autocomplete($search, ['types' => 'address']);

return collect($places)
->mapWithKeys(function ($place) {
return [$place->placeId() => $place->description()];
})->toArray();
});
}
}
My problem now is how will I populate or display my label as the address from place api with the places id saved on the column?
No description
3 Replies
Dennis Koch
Dennis Kochβ€’5mo ago
Have a look at this trick I wrote some time ago. That should point you in the right direction: Ahh, maybe not as mine populates other fields πŸ˜… https://v2.filamentphp.com/tricks/geocoding-field-using-select-component
Filament
Geocoding field using Select component by Dennis Koch - Tricks - Fi...
Filament is a collection of tools for rapidly building beautiful TALL stack apps, designed for humans.
Dennis Koch
Dennis Kochβ€’5mo ago
There should be a getOptionLabel() or similar method to get the label for the saved method
ronssij
ronssijβ€’5mo ago
@Dennis Koch Yes I found the solution on the using the getOptionLabel() Thank you
$this->getOptionLabelUsing(static function (GooglePlaceField $component, $state) {
$place = Places::getPlaceDetails($state);

return $place->formatted();
})
$this->getOptionLabelUsing(static function (GooglePlaceField $component, $state) {
$place = Places::getPlaceDetails($state);

return $place->formatted();
})