F
Filament5mo ago
Pints

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:
```php Repeater::make('products') ->live() ->schema([ TextInput::make('quantity')...
Jump to solution
6 Replies
toeknee
toeknee5mo ago
it s a repeater so an array, so get the products then for each you want the quanity and * by the price per item
Pints
PintsOP5mo ago
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
toeknee
toeknee5mo ago
Of course.... quantity and price are in an array of multiple values so $get('quantity') how would we know which one to get?
TextInput::make('amount')
->default(function (Get $get) {
$amount = 0;
foreach($get('products', []) as $product) {
$amount += ((int) $product['quanity'] * (float) $product['price']);
}

return $amount;
}),
TextInput::make('amount')
->default(function (Get $get) {
$amount = 0;
foreach($get('products', []) as $product) {
$amount += ((int) $product['quanity'] * (float) $product['price']);
}

return $amount;
}),
Pints
PintsOP5mo ago
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
toeknee
toeknee5mo ago
Repeater::make('products')
->live()
->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);
})

])
->afterStateUpdated(function (Get $get, Set $set) {
$amount = 0;
foreach($get('products', []) as $product) {
$amount += ((int) $product['quanity'] * (float) $product['price']);
}

$set('amount', $amount);
}),
Repeater::make('products')
->live()
->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);
})

])
->afterStateUpdated(function (Get $get, Set $set) {
$amount = 0;
foreach($get('products', []) as $product) {
$amount += ((int) $product['quanity'] * (float) $product['price']);
}

$set('amount', $amount);
}),
Pints
PintsOP5mo ago
Thank you so much sir! I didn't know it can be done like that.
Want results from more Discord servers?
Add your server