Guava Calendar使用Resource展示医生预约事件失败求助
排查Guava Calendar资源视图事件不显示问题
以下是几个关键排查方向:
1. 修正Doctor模型的toResource方法
你当前在toResource中返回的是当前用户的名称,而非医生自身的标识,这会导致资源匹配错误。应使用Doctor模型自身的字段:
public function toResource(): array|Resource { return Resource::make($this->id) ->title($this->name); // 替换为Doctor模型中存储医生名称的字段 }
2. 优化Appointment模型的toEvent方法
- 无需重复查询
Appointment::find($this->id),直接传入当前实例$this即可 - 资源ID应使用预约关联的医生ID(而非依赖当前登录用户的医生ID),避免上下文错误:
public function toEvent(): array|Event { return Event::make($this) ->title($this->reference . ' - ' . $this->patient->full_name) ->start($this->start_at) ->end($this->end_at) ->resourceId($this->doctor_id) // 使用预约自身关联的doctor_id字段 ->action('view'); }
3. 修正Widget的getResources返回格式
插件要求getResources返回Resource实例集合,而非纯数组。应调用Doctor模型的toResource()方法生成正确的资源对象:
public function getResources(): Collection|array { return Doctor::query() ->where('user_id', auth()->id()) ->get() ->map(fn ($doctor) => $doctor->toResource())->all(); }
4. 验证数据关联与权限
- 确认当前登录用户与Doctor模型的关联关系正确(比如User模型中存在
hasOne Doctor的定义) - 检查
getEvents中auth()->user()->doctor->appointments()是否能正确获取到预约数据,可临时添加调试代码确认:
// 在getEvents中临时添加调试代码 $appointments = auth()->user()->doctor->appointments()->with(['patient'])->get(); dd($appointments); // 确认是否有数据返回
5. 检查时间字段格式
确保start_at和end_at是Carbon实例或插件可识别的时间格式(比如ISO 8601),如果数据库字段类型不是datetime/timestamp,需手动转换:
->start($this->start_at->toIso8601String()) ->end($this->end_at->toIso8601String())
内容的提问来源于stack exchange,提问作者Abdo Rabah
相关产品推荐
相关产品推荐

