Afterstate update
i want to use aftestatte update, and then i got an error "Cannot access offset of type string on string" in $selectedId = $state['playstation_id'];
this is my full code
Select::make('playstation_id')
->label('Nama Playstation')
->relationship('playstation', 'name')
->reactive()
->afterStateUpdated(function (Closure $set, $state) {
$selectedId = $state['playstation_id'];
$selectedPlaystation = Playstation::find($selectedId);
if ($selectedPlaystation) {
$price = $selectedPlaystation->price;
} else {
$price = 0;
}
$set('price', $price);
})
hope u help me, thanks
4 Replies
i want to update automaticaly text price if i select the playtstaion
you can try dd($state);
I think you don't need ['playstation_id'] because $state get the value selected in current component.
If you get anything other than an ID with dd($state), the problem is with your relationship()
thanks, i've done it
how can i get the value if i want to use the value and calculated him in another input
You can have 3 states:
- Set $set
- Get $get
- $state
It works this way:
- $set('component_name', 'new_value') change component value from anywhere
- $get('component_name') retrieves component value from anywhere
- $state retrieves the value of the current component
Note that when you find yourself nested in a Repeater for example, the path for $get and $set can be the following:
- $get('../../component_name')
So if you want to use the value of your Playstation_id in your Price you will have something like this:
$playstation = Playstation::find($get('playstation_id'));
$set('price', $playstation->price);
Be careful, remember to put the methods ->reactive() on your components which must be edited directly otherwise you will surely not see the changes.