FilamentF
Filament11mo ago
Picolé

how to set the value of an input inside of a repeater?

So lets say I have a field inside a repeater and a repeater

I want to change a value of an input inside of the repeater after another field inside of the repeater value changes

I tried many approaches and the closest one to work was this one

public static function recalculateAllValueCustomers($livewire): void
    {
        $statePath = 'data';
        $products = data_get($livewire, $statePath . '.products') ?? [];

        // Retrieve parent fields from the state
        $oceanFreight = (float) data_get($livewire, $statePath . '.ocean_freight', 0);
        $tpFactor = (float) data_get($livewire, $statePath . '.value_customer_percentage', 0);

        // Count only non-empty products (a product is non-empty if its "value" is not empty)
        $nonEmptyProducts = collect($products)
            ->filter(fn($item) => isset($item['value']) && $item['value'] !== '')
            ->all();
        $totalProducts = count($nonEmptyProducts) ?: 1;

        // Update each product's "value_customer" in place
        foreach ($products as $key => $product) {
            if (empty($product['value'])) {
                continue;
            }
            $unitaryPrice = (float) $product['value'];
            $valueCustomer = ($oceanFreight / $totalProducts) + ($unitaryPrice * $tpFactor);
            $formattedValue = number_format($valueCustomer, 2, '.', '');
            // Only update if changed to avoid unnecessary refreshes
            if (!isset($product['value_customer']) || $product['value_customer'] !== $formattedValue) {
                $products[$key]['value_customer'] = $formattedValue;
            }
        }

        // Update the Livewire state in place without re-indexing.
        data_set($livewire, $statePath . '.products', $products);
    }


with this approach, I think it replaces the whole products repeater, so it only work 1 time because the fields inside of the repeater lose it's reactivity

how to actually change it?
Was this page helpful?