© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
FilamentF
Filament•3y ago•
4 replies
sanis

Translatable plugin doesn't support relationship selects

I'm trying to implement a dynamic form where I have select with relationship which has translatable title. However, there is no way to pass the name except passing labels like this:

Select::make('category_uuid')
    ->relationship('category', 'name')
    ->getOptionLabelFromRecordUsing(fn($record) => $record->name)
    ->preload(),
Select::make('category_uuid')
    ->relationship('category', 'name')
    ->getOptionLabelFromRecordUsing(fn($record) => $record->name)
    ->preload(),


So I made some changes in the select component near where options are formed by using function above:

            if (in_array(HasTranslations::class, class_uses_recursive($relationship->getRelated())) && !$component->hasOptionLabelFromRecordUsingCallback()) {
                if (in_array($relationshipTitleAttribute, $relationship->getRelated()->getTranslatableAttributes())) {
                    return $relationshipQuery
                        ->get()
                        ->mapWithKeys(static fn (Model $record) => [
                            $record->{Str::afterLast($keyName, '.')} => $record->getTranslation($relationshipTitleAttribute, $component->getContainer()->getLivewire()->getActiveFormsLocale())
                        ])
                        ->toArray();
                }
            }

            // below goes original code

            if ($component->hasOptionLabelFromRecordUsingCallback()) {
                return $relationshipQuery
                    ->get()
                    ->mapWithKeys(static fn (Model $record) => [
                        $record->{Str::afterLast($keyName, '.')} => $component->getOptionLabelFromRecord($record),
                    ])
                    ->toArray();
            }
            if (in_array(HasTranslations::class, class_uses_recursive($relationship->getRelated())) && !$component->hasOptionLabelFromRecordUsingCallback()) {
                if (in_array($relationshipTitleAttribute, $relationship->getRelated()->getTranslatableAttributes())) {
                    return $relationshipQuery
                        ->get()
                        ->mapWithKeys(static fn (Model $record) => [
                            $record->{Str::afterLast($keyName, '.')} => $record->getTranslation($relationshipTitleAttribute, $component->getContainer()->getLivewire()->getActiveFormsLocale())
                        ])
                        ->toArray();
                }
            }

            // below goes original code

            if ($component->hasOptionLabelFromRecordUsingCallback()) {
                return $relationshipQuery
                    ->get()
                    ->mapWithKeys(static fn (Model $record) => [
                        $record->{Str::afterLast($keyName, '.')} => $component->getOptionLabelFromRecord($record),
                    ])
                    ->toArray();
            }


Is there a better way to get this supported by default from plugin or by the filament form builder itself? Because from my point of view now I'm locked with current version of the Select component, because this hook is in the middle of the big callbacks hell, which forces me to reuse quite a lot of code which I don't need.
Filament banner
FilamentJoin
A powerful open source UI framework for Laravel • Build and ship admin panels & apps fast with Livewire
20,307Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

Spatie Translatable Plugin and relationship fieldset
FilamentFFilament / ❓┊help
2y ago
Translatable plugin : make relationship field works
FilamentFFilament / ❓┊help
3y ago
Translatable Plugin - getLocaleLabelUsing
FilamentFFilament / ❓┊help
15mo ago
Tags & Translatable Plugin
FilamentFFilament / ❓┊help
2y ago