F
Filament3mo ago
Selva

Get the parent id inside child repeater form

I have a nested form using repeater like, main form -> child repeater form -> grandchild repeater form. Here I need to take the id of main form and insert into grandchild table as main_form_id. I have tried few solutions but not working, This is my form,
return $form
->schema([
// Main form
Forms\Components\TextInput::make('name')->required()->maxLength(255),

// child form
Forms\Components\Repeater::make('groups')->relationship()->schema([
Forms\Components\TextInput::make('name')->required(),

// grand child form
Forms\Components\Repeater::make('requests')->relationship()->schema([
Forms\Components\TextInput::make('name')->required(),
])->mutateRelationshipDataBeforeCreateUsing(function (array $data, Get $get) {
// $data['project_id'] = $get('../../id');
// dd($get());
// dd($data);
return $data;
}),
])
]);
return $form
->schema([
// Main form
Forms\Components\TextInput::make('name')->required()->maxLength(255),

// child form
Forms\Components\Repeater::make('groups')->relationship()->schema([
Forms\Components\TextInput::make('name')->required(),

// grand child form
Forms\Components\Repeater::make('requests')->relationship()->schema([
Forms\Components\TextInput::make('name')->required(),
])->mutateRelationshipDataBeforeCreateUsing(function (array $data, Get $get) {
// $data['project_id'] = $get('../../id');
// dd($get());
// dd($data);
return $data;
}),
])
]);
5 Replies
toeknee
toeknee3mo ago
I think if you pass $livewire into the function you can call $livewire->ownerRecord or parentRecord ?
Selva
SelvaOP3mo ago
I am trying to include livewire here, Is this the way to include, I am newbie for this,
use Filament\Forms\Components\Livewire;
->mutateRelationshipDataBeforeCreateUsing(function (array $data, Livewire $livewire): array {
dd($livewire);
return $data;
})
use Filament\Forms\Components\Livewire;
->mutateRelationshipDataBeforeCreateUsing(function (array $data, Livewire $livewire): array {
dd($livewire);
return $data;
})
toeknee
toeknee3mo ago
try:
use Filament\Forms\Components\Livewire;
->mutateRelationshipDataBeforeCreateUsing(function (array $data, $livewire): array {
dd($livewire);
return $data;
})
use Filament\Forms\Components\Livewire;
->mutateRelationshipDataBeforeCreateUsing(function (array $data, $livewire): array {
dd($livewire);
return $data;
})
Selva
SelvaOP3mo ago
That's great!. Thank you for your valuable support. Here is the working code,
->mutateRelationshipDataBeforeCreateUsing(function (array $data, $livewire): array {
$data['parent_id'] = $livewire->record->id;
return $data;
})
->mutateRelationshipDataBeforeCreateUsing(function (array $data, $livewire): array {
$data['parent_id'] = $livewire->record->id;
return $data;
})
toeknee
toeknee3mo ago
Great stuff!
Want results from more Discord servers?
Add your server