F
Filament8mo ago
WEBMAS

How to make a custom titleAttribute in Select::make('course_id')->relationship ?

Hello I want to change titleAttribute. I need to display additional fields in the option names. Select::make('course_id') ->relationship('course', 'number') ->searchable() I want for example: ->relationship('course', 'number title year') How to do it? And the search also needs to work by full name.
13 Replies
lazydog
lazydog8mo ago
use options instead and a callback and specify your table columns. Then in your ->searchable([columns])
WEBMAS
WEBMASOP8mo ago
I have millions of records. If I use options will it work well?
lazydog
lazydog8mo ago
if you have millions of records, use this getOptionLabelUsing and getSearchResultsUsing https://filamentphp.com/docs/3.x/forms/fields/select#returning-custom-search-results
WEBMAS
WEBMASOP8mo ago
How to use getTitle() in getSearchResultsUsing? getTitle() is my custom function in the Course model. ->getSearchResultsUsing(fn(string $search, Select $component): array => Course::whereAny($component->getSearchColumns(), 'like', "%{$search}%")->limit(50)->pluck('title', 'id')->toArray()) ->getOptionLabelUsing(fn($value): ?string => Course::find($value)->getTitle())
lazydog
lazydog8mo ago
You can customize it and return an array format with your custom format data
WEBMAS
WEBMASOP8mo ago
Can you make a working example?
lazydog
lazydog8mo ago
Forms\Components\Select::make('geographic_origin')
->reactive()
->getSearchResultsUsing(function (string $search) {
return [
'one' => 'One hundred',
'two' => 'Two hundred',
'three' => 'Three hundred',
];
})
->getOptionLabelUsing(function (string $value) {
return [
'one' => 'One hundred label',
'two' => 'Two hundred label',
'three' => 'Three hundred label',
];
})
->searchable()
->preload()
Forms\Components\Select::make('geographic_origin')
->reactive()
->getSearchResultsUsing(function (string $search) {
return [
'one' => 'One hundred',
'two' => 'Two hundred',
'three' => 'Three hundred',
];
})
->getOptionLabelUsing(function (string $value) {
return [
'one' => 'One hundred label',
'two' => 'Two hundred label',
'three' => 'Three hundred label',
];
})
->searchable()
->preload()
WEBMAS
WEBMASOP8mo ago
This is a manual array. Look at my code: ->getSearchResultsUsing(fn(string $search, Select $component): array => Course::whereAny($component->getSearchColumns(), 'like', "%{$search}%")->limit(50) ->pluck('title', 'id')->toArray()) ->pluck('title', 'id') title as the option name. I want to make a combined title like 'title year' The best way is to use my getTitle() function How to do it?
lazydog
lazydog8mo ago
->getSearchResultsUsing(function (string $search) {
$courses = Course::whereAny($component->getSearchColumns(), 'like', "%{$search}%")->limit(50)->get();
$data[];
foreach($courses as $course) {
$data[$course->id] = $course->title . ' ' . $course->year;
}
return $data;
})
->getSearchResultsUsing(function (string $search) {
$courses = Course::whereAny($component->getSearchColumns(), 'like', "%{$search}%")->limit(50)->get();
$data[];
foreach($courses as $course) {
$data[$course->id] = $course->title . ' ' . $course->year;
}
return $data;
})
WEBMAS
WEBMASOP8mo ago
OK. I think this can be done somehow easier. Thank you.
lazydog
lazydog8mo ago
you can play around with this
krekas
krekas8mo ago
instead of foreach i think mapWithKeys could be used if you prefer collections
WEBMAS
WEBMASOP8mo ago
Correct solution to the problem: Select::make('course_id') ->relationship('course', 'title') ->getOptionLabelFromRecordUsing(fn(Course $record) => $record->getTitle()) ->searchable(['title', 'number', 'year'])
Want results from more Discord servers?
Add your server