我正在尝试在已加载的hasmany关系上加载hasone关系。
基本上,我已经加载了完成日志,但是还想为每个已加载的完成日志记录加载pool_section模型关系。
$appointments = Appointment::primaryAppointments()->ofReportingPeriod($reportingPeriod->id)->take(5)->get();
$appointments = $appointments->load('profile', 'department')->load(['completion_logs' => function ($query) use ($reportingPeriod) {
$query->where('reporting_period_id', $reportingPeriod->id)->whereNotNull('pool_section_id')->orderBy('pool_section_id');
}]);
尝试从上面将pool_section关系加载到已加载的完成日志上:
class PoolSectionCompletion extends Model
public function pool_section()
{
return $this->belongsTo('App\Models\PoolSection', 'pool_section_id', 'id');
}
class Appointment extends Model
public function completion_logs()
{
return $this->hasManyThrough('App\Models\PoolSectionCompletion', 'App\Models\Profile', 'id', 'profile_id', 'profile_id', 'id');
}
我正在尝试加载选择的完成日志,然后基于id加载pool_section关系,以便获取每个已加载的完成日志的pool_section数据。任何帮助表示赞赏。
看来,如果我按如下所示添加关系-映射正确,但会引入每个报告期的所有完成日志,而不仅仅是每个路线定义的日志。我需要已定义报告期的完成日志,但与pool_section具有映射的hasone关系。
$appointments = $appointments->load('profile', 'department')->load(['completion_logs' => function ($query) use ($reportingPeriod) {
$query->where('reporting_period_id', $reportingPeriod->id)->whereNotNull('pool_section_id')->orderBy('pool_section_id');
}])->load('completion_logs.pool_section');
答案 0 :(得分:1)
它永远不会失败...花数天/小时的时间来尝试解决问题,然后最终发布问题,以便在经过一些额外的努力后几个小时后才能解决它。
只需将关系加载到hasmany关系中,我就能实现所需的目标。
class Appointment extends Model
public function completion_logs()
{
return $this->hasManyThrough('App\Models\PoolSectionCompletion', 'App\Models\Profile', 'id', 'profile_id', 'profile_id', 'id')->with('pool_section');
}