F
Filament8mo ago
Luiz

Form Tabs not saving the correct relationship content.

So I have a form that has an Tab that handles the translation of some fields, like title and description of a model. What is happening is that for now the app has 3 basic languages, more will be add later. To make this possible every translatable model has an i18n table counterpart. This i18n has one column per translatable property of the model together with the composite primary key model_id and locale, here is an example:
Table public.models
- id [pk]
- status
- created_at
- updated_at

Table i18n.models
- model_id [pk] [fk public.models.id]
- locale [pk]
- title
- description
Table public.models
- id [pk]
- status
- created_at
- updated_at

Table i18n.models
- model_id [pk] [fk public.models.id]
- locale [pk]
- title
- description
The example above shows that Model will not have title and description fields. It will be saved on the i18n one with their respective locale, en_CA, fr_CA and etc. I built and trait that helps getting the correct model with above structure. Trait What happens is that the correct values are shown, but when I press the save button, the current visible Tab overwrites all others locales. So if a save when en_CA tab is visible, the tab holding the values for fr_CA will be overwritten. Here is an simplified code of how Tabs was built:
class ExampleResource extends Resource
{
public static function form(Form $form): Form
{
return $form
->schema([
Section::make('Core information')
->schema([
Tabs::make()
->tabs([
static::createLanguageTab('en_CA'),
static::createLanguageTab('fr_CA'),
]),
]),
]);
}

private static function createLanguageTab($locale): Tabs\Tab
{
return Tab::make($locale)
->schema([
Group::make()
/** this will call the magic methods `intlEnCA` and `intlFrCA` */
->relationship('intl' . ucfirst(preg_replace('/_/', '', $locale)))
->schema([
/** Necessary to create a new row */
Components\Hidden::make('locale')->default($locale),
Components\TextInput::make('title')->label('Title'),
Components\TextInput::make('description')->label('description'),
]),
]);
}
}
class ExampleResource extends Resource
{
public static function form(Form $form): Form
{
return $form
->schema([
Section::make('Core information')
->schema([
Tabs::make()
->tabs([
static::createLanguageTab('en_CA'),
static::createLanguageTab('fr_CA'),
]),
]),
]);
}

private static function createLanguageTab($locale): Tabs\Tab
{
return Tab::make($locale)
->schema([
Group::make()
/** this will call the magic methods `intlEnCA` and `intlFrCA` */
->relationship('intl' . ucfirst(preg_replace('/_/', '', $locale)))
->schema([
/** Necessary to create a new row */
Components\Hidden::make('locale')->default($locale),
Components\TextInput::make('title')->label('Title'),
Components\TextInput::make('description')->label('description'),
]),
]);
}
}
Gist
Laravel Translatable model with magic method
Laravel Translatable model with magic method. GitHub Gist: instantly share code, notes, and snippets.
0 Replies
No replies yetBe the first to reply to this messageJoin