how to populate a repeater with existing data?

How can I populate a repeater with existing data? This is on a custom livewire component.

    #[On('setSelectedDocument')]
    public function setSelectedDocument($id)
    {
        $this->uploadId = $id;
        $this->lineItems = DocumentUploadLineItem::where('document_upload_id', $this->uploadId)->get();
        // $this->data = ???
        // $this->form->fill(); ??
    }

    public function form(Form $form): Form
    {
        return $form->schema([
            Repeater::make('lineItems')
                ->label('')
                ->schema([

                    Select::make('account_ref_id')
                        ->required()
                        ->label('Account')
                        ->options($this->getAccounts($this->location))
                        ->preload()
                        ->searchable()
                        ->inlineLabel()
                        ->loadingMessage('Loading accounts...'),

                    TextInput::make('total')
                        ->required()
                        ->inlineLabel()
                        ->prefix('$')
                        ->numeric()
                        ->minValue(.01)
                        ->afterStateHydrated(function (TextInput $component, ?string $state) {
                            $component->state($this->toCurrency($state));
                        })
                ])
                ->minItems(2)
                ->maxItems(5)
                ->defaultItems(2)
                ->addActionLabel('Add Line Item')
                ->columns(2)
                ->reorderable(false)
        ])->statePath('data');
    }
Solution
Use code similar to the following in the mount() method:
$this->form->fill([
    'lineItems' => [
        ['account_ref_id' => '1', 'total' => '100.00'],
        ['account_ref_id' => '2', 'total' => '200.00'],
    ],
]);
Was this page helpful?