所以,我可以有多个 'auth' 表,比如 'patients'、'users' 或 'patient_groups'
我需要通过多态 ScheduledCalendarItem 关系与锻炼会话、问卷或活动相关联。通过下面的关系,我实际上可以获得预定项目,但我希望目标 BelongsToMany 模型具有“透视”数据(例如,预定日历项目中的预期日期和完成时间)
public function scheduledCalendarItems(): MorphMany
{
return $this->morphMany(ScheduledCalendarItem::class, 'target_model');
}
public function exerciseSessions(): MorphMany
{
return $this->scheduledCalendarItems()
->withTrashed()
->where('scheduled_model_type', ExerciseSession::class);
}
使用此代码,我可以获得 ScheduledCalendarItem 记录,但在这种情况下,我实际上需要ExerciseSession 模型(例如通过Morph 的belongsToMany),并且仍然从scheduled_calendar_items 中获取数据expected_date 和compelted_at。
我当前的(无效)输出:
我真正想要的输出:
这是我的数据库的图表,它应该如何工作..
有谁知道如何在 Laravel 中做到这一点?我正在运行 Laravel 9。