You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何实时更新wire:model值,自动计算预约结束时间

解决实时更新预约结束时间的问题

问题根源

当前代码未监听开始时间的实时变化,也没有在开始时间更新时自动重新计算结束时间,导致无法实现实时同步效果。

解决方案

1. 在Livewire组件中添加更新钩子

在组件类中添加updatedStartAppointment方法,每当start_appointment字段更新时,自动计算并更新end_appointment:

// 当start_appointment更新时触发
public function updatedStartAppointment()
{
    // 确保选择了日期,若无则用当前日期(可根据业务需求调整)
    if (empty($this->chooseDate)) {
        $this->chooseDate = Carbon::today()->toDateString();
    }
    
    // 解析开始时间并添加30分钟,格式化为H:i
    $startTime = Carbon::parse("{$this->chooseDate} {$this->start_appointment}");
    $this->end_appointment = $startTime->addMinutes(30)->format('H:i');
}

2. 调整视图中的Wire绑定

  • 给开始时间输入框添加wire:model.live,确保实时触发组件更新
  • 结束时间输入框去掉wire:model.lazy,保持普通wire:model即可(因输入框为disabled,仅需单向同步)

修改后的视图代码:

<div class="grid md:grid-cols-2 gap-x-2">
    <div class="mb-4">
        <label class="block mb-1"> Appointment Start Time </label>
        <div class="relative">
            <div class="flex items-center justify-center">
                <div class="datepicker relative form-floating mb-3 xl:w-96">
                    <input wire:model.live="start_appointment" type="time" class="form-control block w-full px-3 py-1.5 text-base font-normal text-gray-700 bg-white bg-clip-padding border border-solid border-gray-300 rounded transition ease-in-out m-0 focus:text-gray-700 focus:bg-white focus:border-blue-600 focus:outline-none" placeholder="Select a date" />
                    <label for="floatingInput" class="text-gray-700">Select a time ex:12:30PM</label>
                    @if($errors->has("start_appointment"))<p style="color:red">{{ $errors->first("start_appointment") }}</p>@endif                      
                </div>
            </div>
        </div>
    </div> 
    <div class="mb-4">
        <label class="block mb-1"> Appointment End Time </label>
        <div class="relative">
            <div class="flex items-center justify-center">
                <div class="datepicker relative form-floating mb-3 xl:w-96">
                    <input wire:model="end_appointment" type="time" disabled class="form-control block w-full px-3 py-1.5 text-base font-normal text-gray-700 bg-white bg-clip-padding border border-solid border-gray-300 rounded transition ease-in-out m-0 focus:text-gray-700 focus:bg-white focus:border-blue-600 focus:outline-none" placeholder="Select a date ex:1:30PM" />
                    <label for="floatingInput" class="text-gray-700"></label>
                    @if($errors->has("end_appointment"))<p style="color:red">{{ $errors->first("end_appointment") }}</p>@endif                      
                </div>
            </div>
        </div>
    </div> 
</div>

3. 修正addApointment方法中的时间转换逻辑

简化原方法中的重复解析步骤,避免逻辑冗余:

public function addApointment($appointmentId){
    // 拼接日期和开始时间,统一解析
    $startDateTime = Carbon::parse("{$this->chooseDate} {$this->start_appointment}");
    $this->start_appointment = $startDateTime->format("Y-m-d H:i:s");
    $this->end_appointment = $startDateTime->addMinutes(30)->format("Y-m-d H:i:s");
    
    $record = Appointments::where([
        ['id',$appointmentId],
        ['business_id',auth()->id()]
    ])->first();

    $record->update([
        'status' => 'in_progress',
        'subject' => $this->subject,
        'description' => $this->description,
        'start_appointment' => $this->start_appointment, 
        'end_appointment' => $this->end_appointment, 
    ]);
}

关键说明

  • updatedStartAppointment是Livewire的生命周期钩子,对应视图中start_appointment字段,会在字段值更新时自动触发
  • wire:model.live确保输入框的变化实时同步到组件,而非等待失去焦点
  • 结束时间输入框设置为disabled,避免用户手动修改,完全由组件计算控制

内容的提问来源于stack exchange,提问作者cdBreak

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 13:20:31