Set the label of a select using $set

In my code I have
->afterStateUpdated(function($state, Set $set) {
if (is_array($state)) {
$set('product_id', $state['id']);
}
})
->afterStateUpdated(function($state, Set $set) {
if (is_array($state)) {
$set('product_id', $state['id']);
}
})
This code sets the value of my Select after a new item has been added using a CreateOptionForm. This succesfully sets the value of the Select. Does anyone know if it is possible to also set the label of the select somehow using the $set method? Thanks
Solution:
You mean the option label. So you want to use getOptionLabelUsing() ?
Jump to solution
8 Replies
Dennis Koch
Dennis Koch4mo ago
$set if for form data. You cannot set the labels. Use a closure ->label(fn () => set label from here) But I think if the option exists inside your select options, the label should be selected automatically?
JJSanders
JJSanders4mo ago
OK I think you misunderstood mee. It's not the label of the select but the label of the value. Maybe it is called optionLabel So In my select I use CreateOptionForm to create a new product. Now when it returns it uses $set to update the value of the select. But how will it set the optionLabel? Nvm I fixed it.
Atai Kulmambetov
Hello @JJSanders How you resolved this? I'm having the same problem. Please help
Dennis Koch
Dennis Koch4mo ago
Show some code and explain what's wrong
Solution
toeknee
toeknee4mo ago
You mean the option label. So you want to use getOptionLabelUsing() ?
JJSanders
JJSanders4mo ago
I ended up using getOptionLabelUsing
Atai Kulmambetov
I have Driver, Vehicle and Contract fields
Select::make('contract_id')
->native(false)
->label('Rental Contract type')
->relationship(name: 'contract', modifyQueryUsing: fn(Builder $query, Get $get) => $query->where('driver_id', $get('driver_id')))
->getOptionLabelFromRecordUsing(function (Contract $contract) {
return $contract->vehicle->body_number . " - " . $contract->title;
})
->preload()
->live(debounce: 50)
->required()
Select::make('contract_id')
->native(false)
->label('Rental Contract type')
->relationship(name: 'contract', modifyQueryUsing: fn(Builder $query, Get $get) => $query->where('driver_id', $get('driver_id')))
->getOptionLabelFromRecordUsing(function (Contract $contract) {
return $contract->vehicle->body_number . " - " . $contract->title;
})
->preload()
->live(debounce: 50)
->required()
The form also has a field for changing the vehicle body number. After this, I need to update the label in the "contract_id" field since its label contains vehicle body number
Dennis Koch
Dennis Koch4mo ago
Hm. Not sure whether that's possible