Laravel Filament:求和计数中继器

Laravel Filament: sum and count repeater

我正在使用 filament 制作表格。我想统计我做的repeater个数,和我输入的值的总和

我想让Total area的默认值是Field area的总和,Number of fields的默认值是统计我生成的Field。看了documentation,我觉得没有这样的功能。但是,我只是想确定是否有实现此目的的技巧。

我尝试 count($get('fields'),但它抛出了一个错误:

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

这是我的代码:

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);
    }

您可以使用 $get 变量并获得中继器 属性。您可以使用 Placeholder 道具并将 content 设置为它。 这是结果

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);
    }

根据 Dan 的 answer 以及我自己的一些修改,我应该使用 placeholder 而不是 default

我的工作代码:

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()
                                ->placeholder(function (Closure $get) { 
                                    $fields = $get('fields');
                                    $sum = 0;
                                    foreach($fields as $field){
                                        foreach ($field as $value){
                                            if ($value == ""){
                                                $value = 0;
                                            }
                                            $sum += $value;
                                        }
                                    }
                                    return $sum;
                                }),
                            TextInput::make("number_of_fields")
                                ->label("Number of Fields")
                                ->disabled()
                                ->placeholder(fn (Closure $get) => count($get('fields'))),
                        ]),

                ])->columnSpan(1),

                Card::make([
                    Select::make("measurement_type")
                        ->label("Measurement Type")
                        ->options(MeasurementType::all()->pluck('name'))
                        ->required(),

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

结果符合预期: