I have three forms created using repeater, requests form which is nested under groups and groups nested under projects (projects -> groups -> requests)
The requests table have fields project_id, group_id, name, etc..., group_id will be auto inserted by repeater but project_id is not inserted automatically because this is a grandparent relation. So, I tried to manually get the project_id from parent but I am not getting the id.
The repeater in Project resource like below,
return $form
->schema([
Forms\Components\TextInput::make('name')->required()->maxLength(255),
Forms\Components\Textarea::make('description')->columnSpanFull(),
Forms\Components\Repeater::make('groups')->relationship()->schema([
Forms\Components\TextInput::make('name')->required(),
Forms\Components\Textarea::make('description'),
Forms\Components\Repeater::make('requests')->relationship()->schema([
Forms\Components\TextInput::make('name')->required(),
Forms\Components\Select::make('type')
->options([
'GET' => 'GET',
'POST' => 'POST',
'DELETE' => 'DELETE',
'PATCH' => 'PATCH',
'PUT' => 'PUT',
])
->required(),
Forms\Components\TextInput::make('endpoint')->required()->columnSpanFull(),
])->orderColumn('sort')->collapsible()->columns(2)->columnSpanFull()
->mutateRelationshipDataBeforeCreateUsing(function (array $data, Get $get): array {
// $data['project_id'] = $get('../../id');
// dd($get);
// dd($data);
return $data;
})
->itemLabel(fn (array $state): ?string => $state['name'] ?? null),
])->orderColumn('sort')->collapsible()->columns(2)->columnSpanFull()
->itemLabel(fn (array $state): ?string => $state['name'] ?? null),
]);
From the above code, the projects data is inserted and groups data is inserted. But while inserting requests data I need to provide project_id which is created above.
Here is the answer for the above question.
By using livewire we can make it possible. The updated code,
->mutateRelationshipDataBeforeCreateUsing(function (array $data, $livewire): array {
// dd($livewire);
$data['project_id'] = $livewire->record->id;
return $data;
})