Change the language of Select Component

I have a form and that form has a Select component that has relationship with department table. The problem that I faced is that when I change the language in create then select data stays in the same language and doesn't change. I use Spatie/Translatable plugin. The component with relationship is:
Forms\Components\Select::make('department')
->relationship('department', 'name')
->preload()
Forms\Components\Select::make('department')
->relationship('department', 'name')
->preload()
Department Model:
use HasTranslations;

public $translatable = [
'name'
];

protected $fillable = [
'name'
];
use HasTranslations;

public $translatable = [
'name'
];

protected $fillable = [
'name'
];
CreateJob.php
use CreateRecord\Concerns\Translatable;

protected static string $resource = JobResource::class;

protected function getActions(): array
{
return [
Actions\LocaleSwitcher::make(),
];
}
use CreateRecord\Concerns\Translatable;

protected static string $resource = JobResource::class;

protected function getActions(): array
{
return [
Actions\LocaleSwitcher::make(),
];
}
Thank you.
9 Replies
toeknee
toeknee17mo ago
Make the select reactive() ?
Algorithm
AlgorithmOP17mo ago
Forms\Components\Select::make('department')
->relationship('department', 'name')
->reactive(),
Forms\Components\Select::make('department')
->relationship('department', 'name')
->reactive(),
Still doesn't work
toeknee
toeknee17mo ago
I’m not sure I totally follow how it’s been implemented but I’m not familiar with that translation plug-in so can’t help much further
Algorithm
AlgorithmOP17mo ago
Filament
Spatie Translatable by Dan Harrin - Plugins - Filament
Filament support for spatie/laravel-translatable.
toeknee
toeknee17mo ago
Ahh ok, I am not too familiar with that I am afraid, you'll need to wait for dan/awocodes/zeppe to check. They are currently at Laracon
Algorithm
AlgorithmOP17mo ago
Ok I will wait.
Andrew Wallo
Andrew Wallo17mo ago
Is that the whole form? Can I see the whole class just to check?
rhukster
rhukster17mo ago
@algorithm7991 @anderson_chaves i'm actually having the exact same issue.. i've made a little progress in that i have the relationship for my case (Event with "hasMany" Categories):
Forms\Components\Select::make('category_id')
->multiple()
->columnSpanFull()
->relationship('categories', 'name->'.app()->getLocale())
->preload()
->required(),
Forms\Components\Select::make('category_id')
->multiple()
->columnSpanFull()
->relationship('categories', 'name->'.app()->getLocale())
->preload()
->required(),
The query being generated is:
select
json_unquote(json_extract(`name`, '$."en"')) as `name->en`,
`id`
from
`categories`
order by
json_unquote(json_extract(`name`, '$."en"')) asc
select
json_unquote(json_extract(`name`, '$."en"')) as `name->en`,
`id`
from
`categories`
order by
json_unquote(json_extract(`name`, '$."en"')) asc
which is correct, the issue is that I cannot get this Select field to refresh when I change the Locale from the dropdown select box. I can't seem to get this field to update, maybe because of the hasMany relationship and category_id is not actually in the Events table? Just FYI, when i manually change the ->relationship('categories', 'name->'.app()->getLocale()) to ->relationship('categories', 'name->de') I do get the German translation, so hardcoding works, but dynamic is not working, but I think purely because it's not 'dirty' and not getting updated by livewire
Algorithm
AlgorithmOP17mo ago
This is the whole function for the form:
public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\Card::make()
->schema([
Forms\Components\TextInput::make('name'),
Forms\Components\RichEditor::make('description'),
])
->columnSpan(2),
Forms\Components\Card::make()
->schema([
Forms\Components\DatePicker::make('schedule'),
Forms\Components\TextInput::make('Location'),
Forms\Components\TextInput::make('salary'),
Forms\Components\Select::make('department')
->relationship('department', 'name')
->reactive(),
])
->columnSpan(1),
])
->columns(3);;
}
public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\Card::make()
->schema([
Forms\Components\TextInput::make('name'),
Forms\Components\RichEditor::make('description'),
])
->columnSpan(2),
Forms\Components\Card::make()
->schema([
Forms\Components\DatePicker::make('schedule'),
Forms\Components\TextInput::make('Location'),
Forms\Components\TextInput::make('salary'),
Forms\Components\Select::make('department')
->relationship('department', 'name')
->reactive(),
])
->columnSpan(1),
])
->columns(3);;
}
Want results from more Discord servers?
Add your server