F
Filament5mo ago
Daniel

Issue: MorphToSelect with Translated Titles

I'm struggling to set up a MorphToSelect field for a polymorphic relationship where I need to display translated titles. Here's my current setup:
MorphToSelect::make('productable')
->types([
MorphToSelect\Type::make(Course::class)
->titleAttribute('id') <-- Here I want to display a column from the related table, I want to display the course_title in the primary language.
->getOptionLabelUsing(function (Course $record): string {
return $record->translations()
->where('locale_id', $record->primary_locale_id)
->first()?->course_title ?? 'Untitled Course';
})
->getSearchResultsUsing(function (string $search): Builder {
return Course::whereHas('translations', function ($query) use ($search) {
$query->where('course_title', 'like', "%{$search}%");
});
}),
])
->native(false)
->required(),
MorphToSelect::make('productable')
->types([
MorphToSelect\Type::make(Course::class)
->titleAttribute('id') <-- Here I want to display a column from the related table, I want to display the course_title in the primary language.
->getOptionLabelUsing(function (Course $record): string {
return $record->translations()
->where('locale_id', $record->primary_locale_id)
->first()?->course_title ?? 'Untitled Course';
})
->getSearchResultsUsing(function (string $search): Builder {
return Course::whereHas('translations', function ($query) use ($search) {
$query->where('course_title', 'like', "%{$search}%");
});
}),
])
->native(false)
->required(),
Courses have translations in a separate table. I can't get the translated title to display in the select field - I either get errors about undefined columns or the ID is displayed instead of the title. How can I properly set this up to: - Display the course title in the primary language. - Allow searching based on translated titles. - Correctly save the polymorphic relationship. Any help is appreciated!
0 Replies
No replies yetBe the first to reply to this messageJoin
Want results from more Discord servers?
Add your server