Select default is acting up

I'm trying to select a default value based on which field is filled:
Select::make('pricing_type')
->label(__('labels.price_is'))
->options([
'per_person' => __('labels.per_person'),
'total' => __('labels.total'),
])
->default(function (?Model $model) {
if ($model) {
return $model->per_person_price !== null ? 'per_person' : 'total';
}
})
->disablePlaceholderSelection(),
Select::make('pricing_type')
->label(__('labels.price_is'))
->options([
'per_person' => __('labels.per_person'),
'total' => __('labels.total'),
])
->default(function (?Model $model) {
if ($model) {
return $model->per_person_price !== null ? 'per_person' : 'total';
}
})
->disablePlaceholderSelection(),
but it always selects the same field, how can i fix this?
5 Replies
awcodes
awcodes2y ago
Two things. $model should be $record. And default only works on create which means there is no record yet so it would just be ‘total’ regardless as the default.
DanielvdSpoel
DanielvdSpoelOP2y ago
hmm okay, but this is not a database field, since it's just a nice ui thing does it also not work if the value is not in the record?
awcodes
awcodes2y ago
default only works on create, which has no record. on edit the default will already have a value therefore default is redundant.
DanielvdSpoel
DanielvdSpoelOP2y ago
okay i guess i need to modify the data then, shouldn't be to big of an issue
awcodes
awcodes2y ago
if you need to change it on edit base on the record you need to look into afterStateHydrated() to set it to what you want.
Want results from more Discord servers?
Add your server