F
Filamentβ€’16mo ago
TheCrazyBob

How do I make a relationship optional?

I have a fieldset with multiple select fields. I want to make sure the relationship is not saved if all the fields inside the fieldset are empty. However, currently Filament is trying to create an empty record for the salary relation. How do I avoid that?
Fieldset::make('listing_salary_id')
->relationship('salary')
->label('Salary Range')
->schema([
Fieldset::make('listing_salary_id')
->relationship('salary')
->label('Salary Range')
->schema([
Solution:
I think there is a saveRelationshipsUsing() or similar method you could use
Jump to solution
9 Replies
Dennis Koch
Dennis Kochβ€’16mo ago
->nullable() ? Ah it's a Fieldset. Not sure whether that works πŸ™ˆ
TheCrazyBob
TheCrazyBobOPβ€’16mo ago
Yeah, that method doesn't exist on Fieldset. I tried ->dehydrated(true) and that doesn't work as well
Dennis Koch
Dennis Kochβ€’16mo ago
It should be ->dehydrated(false) if you don't want to save it But then you need to pass a condition via a closure
TheCrazyBob
TheCrazyBobOPβ€’16mo ago
Doesn't work. It still tries to create a record with empty values.
Solution
Dennis Koch
Dennis Kochβ€’16mo ago
I think there is a saveRelationshipsUsing() or similar method you could use
TheCrazyBob
TheCrazyBobOPβ€’16mo ago
Is there an example for that in the docs?
Dennis Koch
Dennis Kochβ€’16mo ago
I have no idea, sorry.
TheCrazyBob
TheCrazyBobOPβ€’16mo ago
I think I fixed it using the saveRelationshipsUsing method:
Dennis Koch
Dennis Kochβ€’16mo ago
Maybe wanna use filled() instead of isset() to filter blank states
Want results from more Discord servers?
Add your server