F
Filament10mo ago
WEBMAS

Multilingual field data is not saved

Hello. I installed and configured this plugin: https://filamentphp.com/plugins/filament-spatie-translatable Set up the model: class Category extends Model { use HasFactory, HasTranslations; public $translatable = ['name']; Configured Resource: public static function form(Form $form): Form { return $form ->schema([ TextInput::make('name.en') ->required(), TextInput::make('name.es') ->required() ... public static function table(Table $table): Table { return $table ->columns([ TextColumn::make('name.en') ->searchable() ->sortable(), TextColumn::make('name.es') ->searchable() ->sortable(), When I create a new category I get the error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'name.en' in 'where clause' SELECT count(*) AS aggregate FROM categories WHERE name.en = ....
How to fix it?
11 Replies
LeandroFerreira
LeandroFerreira10mo ago
you should use TextInput::make('name')
WEBMAS
WEBMASOP10mo ago
But then I won’t be able to have both fields at once in one form. I don't want to use the language switcher to select a field. I want two fields on one form at once. And in the table.
LeandroFerreira
LeandroFerreira10mo ago
I think the problem is because you are using searchable and sortable
WEBMAS
WEBMASOP10mo ago
I can remove fields from public static function table(Table $table). But there is an error when adding a new entry using public static function form(Form $form) Therefore searchable and sortable do not affect the problem( I solved the problem with saving data. It was necessary to remove ->unique() from the field. The problem that remains is the table. It does not display the contents of the field.
LeandroFerreira
LeandroFerreira10mo ago
Not sure if it is the right way..
->state(fn(Model $record):string => $record->getTranslations('name')['es'])
->state(fn(Model $record):string => $record->getTranslations('name')['es'])
WEBMAS
WEBMASOP10mo ago
It works. Thank you. Can I get the language code specified in make('name.en')? TextColumn::make('name.en') ->label(__('name') . ...getLocale()) ->state(fn(Model $record):string => $record->getTranslations('name')[...getLocale()])
LeandroFerreira
LeandroFerreira10mo ago
Inject $column and use $column->getName()
WEBMAS
WEBMASOP10mo ago
Thank you. Here's the final code: TextColumn::make('name.en') ->label(fn(TextColumn $column) => __('category.name') . ' (' . str($column->getName())->afterLast('.') . ') ') ->state(fn(Category $record, TextColumn $column): string => $record->getTranslations(str($column->getName())->beforeLast('.'))[str($column->getName())->afterLast('.')->toString()]) ->searchable() ->sortable(),
LeandroFerreira
LeandroFerreira10mo ago
why not TextColumn::make('en') and use $column->getName() ?
WEBMAS
WEBMASOP10mo ago
In this case, sorting does not work. And if TextColumn::make('name.en') then it works I have a feeling that this is being done somehow easier. It seems to me that our solution is complicated.
LeandroFerreira
LeandroFerreira10mo ago
me too

Did you find this page helpful?