我在laravel中遇到了“急切加载”的问题。
我正在处理相互关联的4个表格。
我有以下模型:
<?php
class AgendaPersonalModel extends Model
{
protected $table = 'agenda_personal';
public function periods() {
return $this->hasMany(AgendaPersonalPeriodModel::class, 'agenda_id');
}
}
?>
class AgendaPersonalPeriodModel extends Model
{
protected $table = 'agenda_personal_period';
public function weekdays() {
return $this->hasMany(AgendaPersonalWeekdaysModel::class, 'period_id');
}
<?php
class AgendaPersonalWeekdaysModel extends Model
{
protected $table = 'agenda_personal_weekdays';
public function breaks() {
return $this->hasMany(AgendaPersonalBreakModel::class, 'weekday_id');
}
}
?>
<?php
class AgendaPersonalBreakModel extends Model
{
protected $table = 'agenda_personal_breaks';
}
?>
现在我想要将所有数据获取到一个对象中。
当我执行以下操作时:
$agendaTest = AgendaPersonalModel::with(array('periods', 'periods.weekdays'))->where('id', 1)->first();
它完美地工作了
但当我做以下操作时:
$agendaTest = AgendaPersonalModel::with(array('periods', 'periods.weekdays', 'weekdays.breaks'))->where('id', 1)->first();
I get the following error:
(1/1) RelationNotFoundException
Call to undefined relationship [weekdays] on model [App\Models\AgendaPersonalModel].
in RelationNotFoundException.php (line 20)
AgendaPersonalModel
中是否定义了工作日的关系?或者你是想使用periods.weekdays.breaks
吗? - Josh