F
Filament9mo ago
Vp

Manually fill simple repeater data value on edit

I'm storing repeater value to another table, each value will be based on languages. On create it's working fine (storing).. but on edit the repeater value cannot display.. what can I do to fill simple repeater value on edit
// form
foreach (Language::all() as $language) {
$fields[] = Forms\Components\Tabs\Tab::make('translations.'.$language->id)
->label($language->name)
->schema([
Forms\Components\TextInput::make('translations.'.$language->id.'.plan_name')
->required(),

Forms\Components\Repeater::make('translations.'.$language->id.'.features')
->simple(
Forms\Components\TextInput::make('translations.'.$language->id.'.features')
->required(),
)
]);
}

// create (working fine)
Actions\CreateAction::make()
->after(function (Model $record, array $data) {
foreach ($data['translations'] as $key => $value) {
$record->translations()->create([
'language_id' => $key,
'plan_name' => $value['plan_name'],
'features' => $value['features'],
]);
}
}),

// edit (it cannot fill repeater data)
Tables\Actions\EditAction::make()
->mutateRecordDataUsing(function (Model $record, array $data): array {
foreach ($record?->translations as $translation) {
$data['translations'][$translation->language_id]['plan_name'] = $translation->plan_name;
$data['translations'][$translation->language_id]['features'] = $translation->features;
}

// dd($data);

return $data;
}),

// relationship
public function translations(): HasMany
{
return $this->hasMany(ProductTranslation::class);
}
// form
foreach (Language::all() as $language) {
$fields[] = Forms\Components\Tabs\Tab::make('translations.'.$language->id)
->label($language->name)
->schema([
Forms\Components\TextInput::make('translations.'.$language->id.'.plan_name')
->required(),

Forms\Components\Repeater::make('translations.'.$language->id.'.features')
->simple(
Forms\Components\TextInput::make('translations.'.$language->id.'.features')
->required(),
)
]);
}

// create (working fine)
Actions\CreateAction::make()
->after(function (Model $record, array $data) {
foreach ($data['translations'] as $key => $value) {
$record->translations()->create([
'language_id' => $key,
'plan_name' => $value['plan_name'],
'features' => $value['features'],
]);
}
}),

// edit (it cannot fill repeater data)
Tables\Actions\EditAction::make()
->mutateRecordDataUsing(function (Model $record, array $data): array {
foreach ($record?->translations as $translation) {
$data['translations'][$translation->language_id]['plan_name'] = $translation->plan_name;
$data['translations'][$translation->language_id]['features'] = $translation->features;
}

// dd($data);

return $data;
}),

// relationship
public function translations(): HasMany
{
return $this->hasMany(ProductTranslation::class);
}
On edit, total rows are correctly populated without value.. and this approach correctly filled text input Plan name
4 Replies
Vp
VpOP9mo ago
This is what I get inside ->mutateRecordDataUsing() before return
No description
Vp
VpOP9mo ago
Solved now
binaryfire
binaryfire9mo ago
@Vp Great to hear you figured it out. Would you mind sharing your solution? It will be useful for others in the community
Vp
VpOP9mo ago
I just change this line (OP) like below and it's working fine
Forms\Components\Repeater::make('translations.'.$language->id.'.features')
->simple(
Forms\Components\TextInput::make('a') // here if I put empty like `make('')` it's not working, I have to put a characters
->required(),
)
Forms\Components\Repeater::make('translations.'.$language->id.'.features')
->simple(
Forms\Components\TextInput::make('a') // here if I put empty like `make('')` it's not working, I have to put a characters
->required(),
)
Want results from more Discord servers?
Add your server