'Laravel Filament: sum and count repeater

I'm making a form using filament. I want to count the number of repeater I make, and the sum of the value I input.

What I expected

I want to make the default value of Total area is the sum of Field area, and the default value of Number of fields is to count the Field I generate. After reading the documentation, I don't think there are such features. But, I just want to make sure if there is a trick to make this happen.

I tried to count($get('fields'), but it thrown an error:

count(): Argument #1 ($value) must be of type Countable|array, null given 

Here's my code:

public static function form(Form $form): Form
    {
        return $form
            ->schema([
                Card::make([
                    Grid::make(2)
                        ->schema([
                            TextInput::make("total_area")
                                ->label("Total area")
                                ->postfix('m²')
                                ->disabled()
                                ->default(fn (Closure $get) => $get('field')),
                            TextInput::make("number_of_field")
                                ->label("Number of fields")
                                ->disabled()
                                ->default(fn (Closure $get) => count($get('fields'))),
                        ]),

                ])->columnSpan(1),

                Card::make([
                    Select::make("measurement_type")
                        ->label("Measurement type")
                        ->required(),

                    Repeater::make('fields')
                        ->label('Field')
                        ->schema([
                            TextInput::make("field")
                                ->label("Field area")
                                ->postfix('m²')
                                ->required(),
                        ])
                ])->columnSpan(1)
            ])->columns(2);
    }


Solution 1:[1]

You can use $get variable and get the repeator Property. And you can use the Placeholder props and Set the content to it. Here is the result

enter image description here

public static  function form(Form $form): Form
    {
        return $form
            ->schema([
                Card::make([
                    Grid::make(2)
                        ->schema([
                            Placeholder::make("total_area")
                                ->label("Total area")
                                ->content(function ($get) {
                                    return collect($get('fields'))
                                        ->pluck('field')
                                        ->sum();
                                }),
                            Placeholder::make("number_of_field")
                                ->label("Number of fields")
                                ->content(function ($get) {
                                    return collect($get('fields'))
                                        ->pluck('field')
                                        ->count();
                                })
                        ]),

                ])->columnSpan(1),

                Card::make([
                    Select::make("measurement_type")
                        ->label("Measurement type")
                        ->required(),

                    Repeater::make('fields')
                        ->label('Field')
                        ->schema([
                            TextInput::make("field")
                                ->label("Field area")
                                ->postfix('m²')
                                ->required()
                                ->reactive(),
                        ])
                ])
                    ->reactive()
                    ->columnSpan(1)
            ])->columns(2);
    }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ManojKiran Appathurai