Best practice for multiple dynamic properties

Looking for advice for the following scenario:
Select::make('product_id')
->options(Product::all()->pluck('name', 'id')),
TextInput::make('quantity')
->numeric()
->step(1)
->maxValue(function(Get $get) {
$productId = $get('product_id');
if (!$productId) {
return;
}

return Product::find($productId)->quanity;
})
->helperText(function(Get $get {
// Basically the same logic as maxValue
return "Available: $stock";
}),
Select::make('product_id')
->options(Product::all()->pluck('name', 'id')),
TextInput::make('quantity')
->numeric()
->step(1)
->maxValue(function(Get $get) {
$productId = $get('product_id');
if (!$productId) {
return;
}

return Product::find($productId)->quanity;
})
->helperText(function(Get $get {
// Basically the same logic as maxValue
return "Available: $stock";
}),
My form also has several places where I will be using the Product data. Do I really need to execute the Product::find($productId) on every dynamic property? Example: After the TextInput::make('quantity') is ->live(onBlur: true) I have another field that will show the Total which is quantity * $product->price . Calling Product::find... for the 3rd time seems a bit much. is there a more efficient approach that I might be not considering, cause calling the Product model every time doesn't seem efficient. Thanks in advance for the advice
2 Replies
H4L1M
H4L1M2mo ago
A resource is just a PHP class ! You can define an array variable And set it's value to your model each time the user changes the selected product with AfterstateUpdated
Want results from more Discord servers?
Add your server