Accessing repeater value from outside
Need help, how do I get the value of those inside the repeater and make the price update every time I made changes inside the repeater?
This is my sample code:
Repeater::make('products')
->schema([
TextInput::make('quantity')
->live()
->default(100)
->afterStateUpdated(function (Set $set, $state) {
$set('quantity', $state);
})
TextInput::make('price')
->live()
->default(100)
->afterStateUpdated(function (Set $set, $state) {
$set('price', $state);
})
]),
TextInput::make('amount')
->default(function (Get $get) {
$quantity = (float) $get('../quantity');
$price = (float) $get('../price');
return $quantity * $price;
}),
It is always null.
Solution:Jump to solution
```php
Repeater::make('products')
->live()
->schema([
TextInput::make('quantity')...
6 Replies
it s a repeater so an array, so get the products then for each you want the quanity and * by the price per item
I don't quite understand your suggestion, I'm sorry
Everytime I dd($get('products')), the result is like this: array:1 [▼ // app\Filament\Resources\PurchaseResource.php:316
"04768f37-5be1-45a2-9df3-90b68ede9137" => array:2 [▼
"quantity" => 100
"price" => 100
]
]
But when I use $get('quantity') or $get('../quantity'), the result is null
Of course.... quantity and price are in an array of multiple values so $get('quantity') how would we know which one to get?
Thank you for this! You're a life saver.
Is it possible to make it reactive or live? When I update the values inside the repeater, it doesn't change automatically
Solution
Thank you so much sir! I didn't know it can be done like that.