F
Filamentβ€’3mo ago
Jellibloom

How to attach and detach a relation in case there were values

My resource may has One to One (Polymorphic) relationship with Review, I want to create a review if I wrote values and if it was null want to delete it if existed
Forms\Components\Fieldset::make(__('Booking review'))
//create only if there are values, if null delete
->relationship('review')
->schema([
Forms\Components\TextInput::make('rating')
->requiredWith('comment'),
Forms\Components\Textarea::make('comment')
->columnSpan(2),
])
->visibleOn('edit')
->visible(function (Forms\Get $get): bool {
return $get('status') === 'in review';
}),
Forms\Components\Fieldset::make(__('Booking review'))
//create only if there are values, if null delete
->relationship('review')
->schema([
Forms\Components\TextInput::make('rating')
->requiredWith('comment'),
Forms\Components\Textarea::make('comment')
->columnSpan(2),
])
->visibleOn('edit')
->visible(function (Forms\Get $get): bool {
return $get('status') === 'in review';
}),
I really appreciate your help folks, really thanks for helping others!
1 Reply
Solution
Jellibloom
Jellibloomβ€’3mo ago
Found the solution πŸ˜„ https://filamentphp.com/docs/3.x/forms/advanced#conditionally-saving-data-to-a-relationship
->relationship(
'review',
fn (?array $state): bool => filled($state['rating'])
)
->relationship(
'review',
fn (?array $state): bool => filled($state['rating'])
)