F
Filament4mo ago
Groli

Set default value for search, or how to resolve ID of a record in select

Hi, I have select that corresponds to One-To-Many relationship and it stores ID of a related record in table of current model, for which form I'm trying to build... But when user loads the page with already selected related record, only ID shows to him and it is not resolved in the getSearchResultsUsing. Do you know how would I be able to resolve this ID that shows to user, I was thinking maybe adding some default search value, but I don't know how to pass current record ID. I provided images with current and expected behaviour. Here is code for the Select:
Select::make('course_id')
->label('Course')
->getSearchResultsUsing(CourseQuery::search(...))
->searchable()
...;
Select::make('course_id')
->label('Course')
->getSearchResultsUsing(CourseQuery::search(...))
->searchable()
...;
I tried using:
->getOptionLabelFromRecordUsing(fn (Course $record) => CourseQuery::formatTitle($record))
->getOptionLabelFromRecordUsing(fn (Course $record) => CourseQuery::formatTitle($record))
but that didn't help.
No description
No description
Solution:
Make the course a relationship on the model and don't use ID. Then you can use ->relationship() on the select which will load everything as expected.
Jump to solution
3 Replies
Solution
toeknee
toeknee4mo ago
Make the course a relationship on the model and don't use ID. Then you can use ->relationship() on the select which will load everything as expected.
Groli
GroliOP4mo ago
But I use custom searching and custom title creation for that model... But adding ->relationship('course') helped me to load the initial data... So thank you for help...
toeknee
toeknee4mo ago
And that's ok for searching, but key/name comes from the relation which is why it works.

Did you find this page helpful?