How to use a Checkbox to edit fields in a related model

I've got a simple has-one relationship in my User model which relates to a UserFeatures model, which has various fields that I'd like to edit with checkboxes in my UserResource:
public function userFeatures(): HasOne
{
return $this->hasOne(UserFeatures::class, 'user_id');
}
public function userFeatures(): HasOne
{
return $this->hasOne(UserFeatures::class, 'user_id');
}
I've tried to do this using 'dot' notation to form a path, where feature_field is one of the fields in UserFeatures, like so:
Checkbox::make('userFeatures.feature_field'),
Checkbox::make('userFeatures.feature_field'),
However, this always returns a state of null. So how can one create checkboxes to edit values in a 'has-one' related model?
4 Replies
Patrick Boivin
Patrick Boivin13mo ago
Maybe this :
Group::make([
Checkbox::make('feature_field'),
])->relationship('userFeatures'),
Group::make([
Checkbox::make('feature_field'),
])->relationship('userFeatures'),
Mike Scott
Mike Scott13mo ago
Unfortunately that doesn't work, since it's a has-one relationship and the one record has many fields which need a checkbox for each field. The relationship method works only when there's a one-to-many relationship and each related record has a single value that gets a checkbox. In my example, feature_field is only one of the boolean fields in the related record. It has about 20 boolean fields.
Patrick Boivin
Patrick Boivin13mo ago
Pretty sure it should work with a HasOne relationship... https://filamentphp.com/docs/2.x/forms/layout#saving-data-to-relationships Include all your checkboxes in the Group
Mike Scott
Mike Scott13mo ago
Aha I just noticed you're suggesting a group relationship. I was confusing it with the CheckboxList relationship. I'll give it a try, thanks!
Want results from more Discord servers?
Add your server
More Posts