F
Filament2mo ago
Sayy

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, ] ); }), ]; }
No description
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);...
Jump to solution
8 Replies
pratik
pratik2mo ago
@Sayy let's dd the product after, $product = Product::find($state); and find out if the product is not null .
Sayy
Sayy2mo ago
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 ?
No description
No description
No description
pratik
pratik2mo ago
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.
Sayy
Sayy2mo ago
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
Sayy
Sayy2mo ago
like this if selected. and if i clear the select field its error like before my image
No description
Sayy
Sayy2mo ago
and how to clear select field ?
Solution
pratik
pratik2mo ago
From as much as I understand, This simple if condition will solve your problem.
->afterStateUpdated(function ($state) {
if($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,
]
);
}
}),
->afterStateUpdated(function ($state) {
if($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,
]
);
}
}),
Sayy
Sayy2mo ago
its true and my problem solve, thanks for help. this if state will handle clear select field right ?