Error when select data with afterstateupdated
after find for select data with afterstateupdated and then error $price null, but my data not null on database.
protected function getFormSchema(): array
{
return [
Select::make('selectedProduct')
->label('Select Product')
->searchable()
->preload()
->options(Product::pluck('name', 'id')->toArray())
->live()
->afterStateUpdated(function ($state) {
$product = Product::find($state);
$this->record->orderDetails()->updateOrCreate(
[
'order_id' => $this->record->id,
'product_id' => $state,
],
[
'product_id' => $state,
'quantity' => $this->quantityValue,
'price' => $product->price,
'subtotal' => $product->price * $this->quantityValue,
]
);
}),
];
}
Solution:Jump to solution
From as much as I understand, This simple if condition will solve your problem.
```php
->afterStateUpdated(function ($state) {
if($state){
$product = Product::find($state);...
8 Replies
@Sayy let's dd the product after, $product = Product::find($state); and find out if the product is not null .
after select product i found my data, and if im clear with X (you can look my image) and then my dd null, how to fix ?
There is no selected product ($state is null) after you clear the select field, what are you trying to achieve when no product is selected ?
you can just check if the $state is not null and wrap the logic in the condition.
after clear the select field the select field can clear and no achieve on order details, and then if product select will on order details
like this if selected. and if i clear the select field its error like before my image
and how to clear select field ?
Solution
From as much as I understand, This simple if condition will solve your problem.
its true and my problem solve, thanks for help. this if state will handle clear select field right ?