【发布时间】:2020-01-18 13:04:42
【问题描述】:
我正在尝试在已加载的 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');
【问题讨论】:
标签: laravel laravel-5 eloquent eloquent-relationship