© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
FilamentF
Filament‱2y ago‱
6 replies
Alexandre

Incremental itemLabel for repeater

Hello everyone. 👋

I know this question has already been asked several times, but I haven't found any answers...

Basically my question is simple: I'm creating a form with a repeater (questions). In this repeater, I've a second one (answers).
And I want to put an itemLabel in this second repeater with an incremental number (Answer 1, Answer 2, Answer 3, etc.).


Repeater::make('questions')
    ->itemLabel(function () {
        static $position = 1;
        return 'Question n°'.$position++;
    })
    ->schema([
        textInput::make('title')
                ->required(),
        Select::make('need_id')
                ->options(Need::all()->pluck('name', 'id'))
                ->required(),
        Repeater::make('answers')
                ->itemLabel(function ($component, array $state, callable $get) {
                    $answers     = $get('answers');
                    $answerKeys  = array_keys($answers);
                   
                    /*And now, I don't know what to do. How to get the current index of actual answer but without                        uuid ?*/ 

                    return 'Réponse n°';
                })
                ->hiddenLabel()
                ->schema([
                    TextInput::make('title')
                            ->required(),
                    TextInput::make('ratio')
                            ->numeric()
                            ->suffix('%')
                            ->default('20')
                            ->required()
                ])
                ->minItems(5)
                ->maxItems(5)
                ->addActionLabel('Ajouter une réponse')
                ->required()
    ])
    ->minItems(2)
    ->maxItems(4)
    ->addActionLabel('Ajouter une question')
    ->collapsible()
Repeater::make('questions')
    ->itemLabel(function () {
        static $position = 1;
        return 'Question n°'.$position++;
    })
    ->schema([
        textInput::make('title')
                ->required(),
        Select::make('need_id')
                ->options(Need::all()->pluck('name', 'id'))
                ->required(),
        Repeater::make('answers')
                ->itemLabel(function ($component, array $state, callable $get) {
                    $answers     = $get('answers');
                    $answerKeys  = array_keys($answers);
                   
                    /*And now, I don't know what to do. How to get the current index of actual answer but without                        uuid ?*/ 

                    return 'Réponse n°';
                })
                ->hiddenLabel()
                ->schema([
                    TextInput::make('title')
                            ->required(),
                    TextInput::make('ratio')
                            ->numeric()
                            ->suffix('%')
                            ->default('20')
                            ->required()
                ])
                ->minItems(5)
                ->maxItems(5)
                ->addActionLabel('Ajouter une réponse')
                ->required()
    ])
    ->minItems(2)
    ->maxItems(4)
    ->addActionLabel('Ajouter une question')
    ->collapsible()


Thanks for you help 😅
Solution
I found a solution on Github for those who are looking too :

->itemLabel(function ($state, $component) {
  $key   = array_search($state, $component->getState());
  $index = array_search($key, array_keys($component->getState()));

  return 'Réponse n°'.$index + 1;
})
->itemLabel(function ($state, $component) {
  $key   = array_search($state, $component->getState());
  $index = array_search($key, array_keys($component->getState()));

  return 'Réponse n°'.$index + 1;
})


And that work like expected đŸ„ł
Source : https://github.com/filamentphp/filament/discussions/8565#discussioncomment-7031649
GitHub
How to add index on Repeater itemLabel ? · filamentphp filament · D...
https://filamentphp.com/docs/3.x/forms/fields/repeater#adding-a-label-to-repeater-items-based-on-their-content ->itemLabel(fn (array $state): ?string => $state['name'] ?? null)
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

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