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,
]
);
}),
];
}
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
From as much as I understand, This simple if condition will solve your problem.