Mohamed Ayaou
Mohamed Ayaou
FFilament
Created by Mohamed Ayaou on 3/16/2025 in #❓┊help
Default value in Repeater items
I am trying to make my repeater items have a default value in one of its fields that increases for each new repeater item. this is a code snippet:
Repeater::make('shipping_infos')
->itemLabel(function () {
static $position = 1;
return $position++;
})
->schema([
TextInput::make('reference')
->label(__('resources.shipments.fields.reference.label'))
->placeholder(__('resources.shipments.fields.reference.placeholder'))
->unique(Shipment::class)
->live()
->default(function () {
static $position = 1;
return 65000 + Shipment::count() + $position;
})
->required(),
// etc ...
])
Repeater::make('shipping_infos')
->itemLabel(function () {
static $position = 1;
return $position++;
})
->schema([
TextInput::make('reference')
->label(__('resources.shipments.fields.reference.label'))
->placeholder(__('resources.shipments.fields.reference.placeholder'))
->unique(Shipment::class)
->live()
->default(function () {
static $position = 1;
return 65000 + Shipment::count() + $position;
})
->required(),
// etc ...
])
the itemLable() is working and increasing for each new item fine, but that didn't work with the default() method (I think since it is not a dynamic method?) and the reference field default value is the same between all the repeater items.
2 replies