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
you should use
TextInput::make('name')
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.
I think the problem is because you are using searchable and sortable
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.
Not sure if it is the right way..
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()])
Inject
$column
and use $column->getName()
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(),
why not
TextColumn::make('en')
and use $column->getName()
?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.
me too