Laravel Filament v3.1 和 Carbon 如何动态更新 PlaceHolder 内容

问题描述 投票:0回答:1

我想计算测量两个输入字段之间时间差异的持续时间。并将结果传递给占位符。

我有一个 Laravel 10.x,在我的模型资源的表单构建器上使用灯丝 PHP 3.1 我有两个 timepiker 字段和一个 PlaceHolder。

我可以做类似的事情,填充文本字段并将其设置为禁用,但我觉得这应该更干净。

但是它不起作用。我也尝试过分割时间实例,无论我做什么,我要么得到一个尾随错误,要么什么都没有发生。

我猜问题是我不能在占位符上使用 SET 。有什么想法吗?

use Carbon\Carbon;
....

Forms\Components\TimePicker::make('startTime')->live()
                      
Forms\Components\TimePicker::make('endTime')
                ->live()
                ->afterStateUpdated(function (Get $get, Set $set) {
                         if ($get('dia_hora_inicio') !== '' && $get('dia_hora_final') !== '') {
                             $startTime = $get('startTime');
                             $endTime = $get('endTime');

                  // Convert starting and ending times to Carbon instances
                            $startDateTime = Carbon::createFromFormat('H:i:s', $startTime);
                            $endDateTime = Carbon::createFromFormat('H:i:s', $endTime);
                  // Calculate the time difference
                           $timeDifference = $endDateTime->diff($startDateTime)->format('%H:%I:%S');
                  // Set the value of the 'duracion' field to the calculated time difference
                                $set('duration', $timeDifference);
                            }
                            return '';
                        }),
 // Target field
 Forms\Components\PlaceHolder::make('duration')
                        ->live()
                        ->visible(function (Get $get) {
                            // Get value from venta_lineas_id
                            $startDateTime = $get('startTime');
                            $endDateTime = $get('endTime');
                            $date = $get('date');
   
                            return $date && ($startDateTime !== '' && $endDateTime !== '');
                        })
                        ->content('foo'),

有什么想法吗?

placeholder formbuilder laravel-10 laravel-filament filamentphp
1个回答
0
投票

嗨,几天前我也有这种类型的要求,所以我为此编写了下面的代码并且它有效

TextInput::make('time')
    ->markAsRequired()
    ->label('Time')
    ->rules('required')
    ->afterStateUpdated(function (Get $get, Set $set) {
         $time = $get('time');
         $duration = (int) $get('duration');
         $end_time = Carbon::parse($time)->addMinutes($duration)->format('H:i:s');
         ($duration >= 1) ? $set('end_time', $end_time) : $set('end_time', '');
    }),
            TextInput::make('duration')
                ->markAsRequired()
                ->numeric()
                ->suffixIcon('heroicon-m-clock')
                ->rules('required')
                ->live()
                ->afterStateUpdated(function (Get $get, Set $set) {
                    $time = $get('time');
                    $duration = (int) $get('duration');
                    $end_time = Carbon::parse($time)->addMinutes($duration)->format('H:i:s');
                    ($duration >= 1) ? $set('end_time', $end_time) : $set('end_time', '');
                }),
            TextInput::make('end_time')
                ->markAsRequired()
                ->readOnly()
                ->rules('required'),
  • 只需根据您的要求更改逻辑即可。
  • 并使用文本输入代替并使其禁用

抱歉我的英语不好

© www.soinside.com 2019 - 2024. All rights reserved.