Translatable plugin : make relationship field works

Hi ! I have a Mission model with a institution() : BelongsTo relationship. On my Mission edit page (resource), I want to show the institution.access_code field but Filament doesn't translate it and try to display the entire object. How can I make it works (display the field in the current locale) ?
Forms\Components\Group::make()
->relationship('institution')
->schema([
Forms\Components\Section::make('Institution')
->schema([
Forms\Components\TextInput::make('access_code')
])
Forms\Components\Group::make()
->relationship('institution')
->schema([
Forms\Components\Section::make('Institution')
->schema([
Forms\Components\TextInput::make('access_code')
])
Solution:
->formatStateUsing(fn (string $state): string => $state['fr']['access_code']) or Forms\Components\TextInput::make('access_code.fr')...
Jump to solution
9 Replies
toeknee
toeknee17mo ago
add ->formatStateUsing(fn(state) => dd($state) to the access_code field, what does it show?
Benjamin
BenjaminOP17mo ago
array:1 [// app\Filament\Resources\MissionsResource.php:74
"fr" => null
]
array:1 [// app\Filament\Resources\MissionsResource.php:74
"fr" => null
]
toeknee
toeknee17mo ago
So check your relatiomnship
Benjamin
BenjaminOP17mo ago
The relationship is correct, the field was just empty. Now I have this :
array:1 [// app\Filament\Resources\MissionsResource.php:74
"fr" => "Une garderie quelconque"
]
array:1 [// app\Filament\Resources\MissionsResource.php:74
"fr" => "Une garderie quelconque"
]
toeknee
toeknee17mo ago
It's object,object because it's received an obejct and not a string so fr = a translation access_code is an access_code
Benjamin
BenjaminOP17mo ago
Yep, I get it. It's automatic for resource fields but not for relationship fields.
Benjamin
BenjaminOP17mo ago
I think I need something like that... I don't know.
->formatStateUsing(fn (string $state): string => $state['fr'])
->formatStateUsing(fn (string $state): string => $state['fr'])
Solution
toeknee
toeknee17mo ago
->formatStateUsing(fn (string $state): string => $state['fr']['access_code']) or Forms\Components\TextInput::make('access_code.fr')
Benjamin
BenjaminOP17mo ago
So weird, I was sure I tried Forms\Components\TextInput::make('access_code.fr') before, but it works now, thanks a lot @toeknee_iom !
Want results from more Discord servers?
Add your server