It seems like you're trying to set a default value for the charge_now field within a Repeater field in Filament. The default() method is used to set the default state for the entire repeater, not for individual fields within the repeater's schema.
If you want to set a default value for charge_now based on another field (due in this case), you should use the default() method on the TextInput itself. However, since you're within a repeater, the context of the default value is dynamic and depends on the current iteration's state.
Here's a revised version of your code that should correctly set the default value for charge_now:
Repeater::make('charges')
->default(function (RelationManager $livewire) {
return $livewire->ownerRecord->charges->where('due', '>', 0)->toArray();
})
->schema([
TextInput::make('item_desc')->label('Charge')->disabled(),
TextInput::make('advertisement')->lazy()->hidden(),
TextInput::make('due')->disabled(),
TextInput::make('amount')->disabled(),
TextInput::make('charge_now')
->integer()
// Use the default() method with a closure that returns the 'due' value
->default(fn (array $get) => $get['due'])
->maxValue(fn (array $get) => $get['due'])
->minValue(0),
])
->addable(false)
->reorderable(false)
->itemLabel(fn (array $state): ?string => 'Advertisement ID - ' . $state['advertisement'] . ' - ' . money($state['charge_now']) ?? null)
->columns(2)
->grid(2)
->collapsible()
->collapsed();
Please note the following changes:
- The
default()method forcharge_nownow uses a closure that takes an array$getas a parameter. This array represents the current state of the repeater item. - The closure uses
$get['due']to access theduevalue directly from the state array.
Make sure that the money() function is defined and can handle null values, as the itemLabel closure may call it with null if charge_now is not set.
If you're still facing issues, ensure that the due field is correctly populated and that the default() method is not being overridden elsewhere in your code.