F
Filamentβ€’2y ago
jelmerkeij

Form builder: Select with getSearchResultsUsing() shows id on form load instead of name

I have a product with relation bundledProducts that indicate the products the main product has as 'children'. Using the form builder I have a seleclist inside a repeater to be able to select these children. The select gets it's options through a function via getSearchResultsUsing():
->schema( [
Forms\Components\Repeater::make( 'bundledProducts' )
->label( __( 'labels.product.plural' ) )
->schema( [
Forms\Components\Select::make( 'bundled_product_id' )
->label( 'Product' )
->searchable()
->getSearchResultsUsing( fn( string $search ) => ProductHelper::searchProducts( $search, onlyProductTypes: [ ProductTypeEnum::DEFAULT->value ] ) )
->getOptionLabelFromRecordUsing( fn( ?Product $record ) => $record->name )
->inlineLabel()
->required(),
] )
] )
->schema( [
Forms\Components\Repeater::make( 'bundledProducts' )
->label( __( 'labels.product.plural' ) )
->schema( [
Forms\Components\Select::make( 'bundled_product_id' )
->label( 'Product' )
->searchable()
->getSearchResultsUsing( fn( string $search ) => ProductHelper::searchProducts( $search, onlyProductTypes: [ ProductTypeEnum::DEFAULT->value ] ) )
->getOptionLabelFromRecordUsing( fn( ?Product $record ) => $record->name )
->inlineLabel()
->required(),
] )
] )
Now ProductHelper::searchProducts() returns an array with the product_id as key and name as value. This works fine when searching for options. But when loading the form the id's of the children are displayed instead of the name (in my example the product has children with id 2 and 4). (please see attachment) I can't figure out what I'm doing wrong here?
1 Reply
jelmerkeij
jelmerkeijOPβ€’2y ago
Ah, I was using the getOptionLabelFromRecordUsing wrong... it doesn't receive a record but instead the value (product_id): ->getOptionLabelUsing(fn ($value): ?string => Product::find($value)?->name) That's working a lot better πŸ™‚
Want results from more Discord servers?
Add your server