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 Koch2y ago
->nullable() ? Ah it's a Fieldset. Not sure whether that works 🙈
TheCrazyBob
TheCrazyBobOP2y ago
Yeah, that method doesn't exist on Fieldset. I tried ->dehydrated(true) and that doesn't work as well
Dennis Koch
Dennis Koch2y 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
TheCrazyBobOP2y ago
Doesn't work. It still tries to create a record with empty values.
Solution
Dennis Koch
Dennis Koch2y ago
I think there is a saveRelationshipsUsing() or similar method you could use
TheCrazyBob
TheCrazyBobOP2y ago
Is there an example for that in the docs?
Dennis Koch
Dennis Koch2y ago
I have no idea, sorry.
TheCrazyBob
TheCrazyBobOP2y ago
I think I fixed it using the saveRelationshipsUsing method:
Dennis Koch
Dennis Koch2y ago
Maybe wanna use filled() instead of isset() to filter blank states

Did you find this page helpful?