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

Livewire联动下拉框回选默认选项触发空属性读取错误求助

问题原因

当重新选择“Choose category”选项时,selectedDoctorCategory被设为空字符串(而非null):

  1. 组件的updatedSelectedDoctorCategory方法中,!is_null($doctor_type_ID)会判定空字符串为true,执行find('')返回null,导致$doctors变为null;
  2. 视图里的@if (!is_null($selectedDoctorCategory))会因为空字符串不是null,继续渲染医生下拉框,此时读取$doctors->doctors就会抛出“attempted to read property doctors on null”错误。
修复方案

1. 修正组件的updatedSelectedDoctorCategory方法

同时处理空字符串和null的情况,未选中分类时重置$doctors为安全的集合:

class CreateAppointment extends Component
{
    public $doctorCategories;
    public $doctors;
    public $selectedDoctorCategory = '';
    
    public function mount(){
        $this->doctorCategories = Doctor_Categories::all();
        $this->doctors = collect();
    }
    
    public function updatedSelectedDoctorCategory($doctor_type_ID)
    {   
        if(!empty($doctor_type_ID)){
            $this->doctors = Doctor_Categories::with('doctors')->find($doctor_type_ID); 
        } else {
            // 重置为集合,避免视图读取null的属性
            $this->doctors = collect();
        }
    }
}

2. 优化视图的判断逻辑

确保只有当分类有效且$doctors存在关联数据时,才渲染医生下拉框:

<div class="form-group row">
    <label for="doctor_categories" class="col-md-4 col-form-label text-md-right">Doctor Categories</label>
    <div class="col-md-6">
        <select wire:model="selectedDoctorCategory" class="form-control">
            <option value="">Choose category</option>
            @foreach($doctorCategories as $cat)
                <option value="{{ $cat->id }}">{{ $cat->doctor_category }}</option>
            @endforeach
        </select>
    </div>
</div>

@if (!empty($selectedDoctorCategory) && $doctors && isset($doctors->doctors))
    <div class="form-group row">
        <label for="doctor" class="col-md-4 col-form-label text-md-right">Doctor</label>
        <div class="col-md-6">
            <select class="form-control" name="doctor_id">
                <option value="" selected>Choose Doctor</option>
                @foreach($doctors->doctors as  $doctor)
                    <option value="{{ $doctor->id }}">{{ $doctor->name }}</option>
                @endforeach
            </select>
        </div>
    </div>
@endif

额外优化建议(更清晰的逻辑)

将$doctors改为直接存储医生集合,避免模型实例和集合的类型混淆:

class CreateAppointment extends Component
{
    public $doctorCategories;
    public $doctors;
    public $selectedDoctorCategory = '';
    
    public function mount(){
        $this->doctorCategories = Doctor_Categories::all();
        $this->doctors = collect();
    }
    
    public function updatedSelectedDoctorCategory($doctor_type_ID)
    {   
        if(!empty($doctor_type_ID)){
            $category = Doctor_Categories::with('doctors')->find($doctor_type_ID);
            $this->doctors = $category->doctors; // 直接存储医生集合
        } else {
            $this->doctors = collect();
        }
    }
}

对应视图修改医生循环部分:

@if (!empty($selectedDoctorCategory) && $doctors->isNotEmpty())
    <div class="form-group row">
        <label for="doctor" class="col-md-4 col-form-label text-md-right">Doctor</label>
        <div class="col-md-6">
            <select class="form-control" name="doctor_id">
                <option value="" selected>Choose Doctor</option>
                @foreach($doctors as  $doctor)
                    <option value="{{ $doctor->id }}">{{ $doctor->name }}</option>
                @endforeach
            </select>
        </div>
    </div>
@endif

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.10 16:31:17