F
Filament7mo ago
Daniel

Help: How to save data of a related model within a Filament form?

I'm trying to make the title of a CourseResource translatable into German and English. However, the translations are not being saved to the course_translations table. How can I handle the saving of fillable attributes that are in another table (relationship between 2 models) ?
public static function form(Form $form): Form
{
return $form->schema([
Section::make('English Content')
->schema([
TextInput::make('translations.en.course_title') <-- Here
->label('Course Title')
->required()
->placeholder('Course Title'),
]),
Section::make('German Content')
->schema([
TextInput::make('translations.de.course_title') <-- Here
->label('Course Title')
->required()
->placeholder('Course Title'),
]),
...
]);
public static function form(Form $form): Form
{
return $form->schema([
Section::make('English Content')
->schema([
TextInput::make('translations.en.course_title') <-- Here
->label('Course Title')
->required()
->placeholder('Course Title'),
]),
Section::make('German Content')
->schema([
TextInput::make('translations.de.course_title') <-- Here
->label('Course Title')
->required()
->placeholder('Course Title'),
]),
...
]);
In the Course model I have this:
public function translations(): HasMany
{
return $this->hasMany(CourseTranslation::class);
}
public function translations(): HasMany
{
return $this->hasMany(CourseTranslation::class);
}
And in the CourseTranslations this:
public function course(): BelongsTo
{
return $this->belongsTo(Course::class);
}
public function course(): BelongsTo
{
return $this->belongsTo(Course::class);
}
Unfortunately Spaties Translatable plugin doesn't suit my needs. :/ The DB tables exist for both both courses and course_translations. Detailed Code with Course Model, CourseTranslation Model and Course Resource: https://gist.github.com/Synistic/b033856ec909badfd09e6573feddb98f
Gist
gist:b033856ec909badfd09e6573feddb98f
GitHub Gist: instantly share code, notes, and snippets.
3 Replies
Daniel
DanielOP7mo ago
Thank you! I managed to setup the repeater. Is it possible to save HasMany relationsships without an repeater? In my case I will always have 2 translations in the repeater, so 2 fixed sections would be better
Kitty
Kitty7mo ago
Want results from more Discord servers?
Add your server