© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
FilamentF
Filament•3y ago•
13 replies
ejoi8

How to "$set" Repeater itemLabel()

Select::make('quantity')
    ->options([
        '1' => 1,
        '2' => 2,
        '3' => 3,
        '4' => 4,
        '5' => 5,
    ])
    ->afterStateUpdated(function (Get $get, Set $set) {

        $name = array_fill(0, $get('quantity'), [
            'name' => '',
        ]);

        $set('members', $name);
    })
    ->live(),

Repeater::make('members')
    ->schema([
        TextInput::make('name')->required()->label('Name'),
    ])
    ->itemLabel("Student")
    ->hidden(fn (Get $get): bool => !$get('quantity'))
    ->addable(false)
    ->columns(1)
    ->collapsible()
    ->deletable(false)
    ->reorderable(false)
    ->live(),
Select::make('quantity')
    ->options([
        '1' => 1,
        '2' => 2,
        '3' => 3,
        '4' => 4,
        '5' => 5,
    ])
    ->afterStateUpdated(function (Get $get, Set $set) {

        $name = array_fill(0, $get('quantity'), [
            'name' => '',
        ]);

        $set('members', $name);
    })
    ->live(),

Repeater::make('members')
    ->schema([
        TextInput::make('name')->required()->label('Name'),
    ])
    ->itemLabel("Student")
    ->hidden(fn (Get $get): bool => !$get('quantity'))
    ->addable(false)
    ->columns(1)
    ->collapsible()
    ->deletable(false)
    ->reorderable(false)
    ->live(),


Hi... I manage to set repeater number base on the
Select::make('quantity')
Select::make('quantity')
but how can I set
itemLabel
itemLabel
in
afterStateUpdated
afterStateUpdated
?

Let say if the selected
quantity
quantity
is 3, the repeater will generate 3
Repeater TextInput
Repeater TextInput
with
itemlabel
itemlabel
value "Student" to all 3. How can I dynamically make it "Student 1", "Student 2", "Student 3"
image.png
Solution
Select::make('quantity')
->options([
    '1' => 1,
    '2' => 2,
    '3' => 3,
    '4' => 4,
    '5' => 5,
])
->afterStateUpdated(function (Get $get, Set $set) {

    $name = array_fill(0, $get('quantity'), [
        'name' => '',
    ]);

    $set('members', $name);
})
->live(),

Repeater::make('members')
->schema([
    TextInput::make('name')->required()->label('Name'),
])
->itemLabel(function (Get $get) {
    foreach ($get('members') as $key => $value) {
        Log::info($get('members'));
        return $key;
    }
})
->hidden(fn (Get $get): bool => !$get('quantity'))
->addable(false)
->columns(1)
->collapsible()
->deletable(false)
->reorderable(false)
->live(),
Select::make('quantity')
->options([
    '1' => 1,
    '2' => 2,
    '3' => 3,
    '4' => 4,
    '5' => 5,
])
->afterStateUpdated(function (Get $get, Set $set) {

    $name = array_fill(0, $get('quantity'), [
        'name' => '',
    ]);

    $set('members', $name);
})
->live(),

Repeater::make('members')
->schema([
    TextInput::make('name')->required()->label('Name'),
])
->itemLabel(function (Get $get) {
    foreach ($get('members') as $key => $value) {
        Log::info($get('members'));
        return $key;
    }
})
->hidden(fn (Get $get): bool => !$get('quantity'))
->addable(false)
->columns(1)
->collapsible()
->deletable(false)
->reorderable(false)
->live(),


I did this but the
foreach
foreach
inside
itemLabel
itemLabel
will repeat entire array. Let say I select the quantity 2, then the loop log (
Log::info($get('members'))
Log::info($get('members'))
) will look something like this

[2023-11-25 02:08:49] local.INFO: array (
  0 => 
  array (
    'name' => '',
  ),
  1 => 
  array (
    'name' => '',
  ),
)  
[2023-11-25 02:08:49] local.INFO: array (
  0 => 
  array (
    'name' => '',
  ),
  1 => 
  array (
    'name' => '',
  ),
)  
[2023-11-25 02:08:49] local.INFO: array (
  0 => 
  array (
    'name' => '',
  ),
  1 => 
  array (
    'name' => '',
  ),
)  
[2023-11-25 02:08:49] local.INFO: array (
  0 => 
  array (
    'name' => '',
  ),
  1 => 
  array (
    'name' => '',
  ),
)  


Lastly, what I did is I add another
id
id
array in
afterStateUpdated
afterStateUpdated
and add another
TextInput::make('id')->hidden()
TextInput::make('id')->hidden()
in the repeater schema then add
->itemLabel(fn (array $state): ?string => 'Student '.$state['id'] ?? null)
->itemLabel(fn (array $state): ?string => 'Student '.$state['id'] ?? null)
. Seem like confusing but here is the full code
Jump to solution
Filament banner
FilamentJoin
A powerful open source UI framework for Laravel • Build and ship admin panels & apps fast with Livewire
20,307Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

Incremental itemLabel for repeater
FilamentFFilament / ❓┊help
2y ago
Custom itemLabel in Repeater
FilamentFFilament / ❓┊help
3y ago
[Test] itemLabel function from Repeater
FilamentFFilament / ❓┊help
12mo ago
ItemLabel Repeater Index In Input
FilamentFFilament / ❓┊help
3y ago