Home > other >  Laravel Filament: sum and count repeater
Laravel Filament: sum and count repeater

Time:01-26

I'm making a form using 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 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);
    }

CodePudding user response:

According to Dan's result

  •  Tags:  
  • Related