php - Laravel 急切加载 4 个表

标签 php laravel eager-loading

我在 laravel 中遇到了'eager loading'的问题。

我正在处理 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();

我收到以下错误:

(1/1) RelationNotFoundException
Call to undefined relationship [weekdays] on model [App\Models\AgendaPersonalModel].
in RelationNotFoundException.php (line 20)

最佳答案

你可以这样做

$agendaTest = AgendaPersonalModel::with(['periods', 'periods.weekdays.breaks'])->where('id', 1)->first();

关于php - Laravel 急切加载 4 个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45185021/

相关文章:

mysql - 在 Rails 3 中急切加载?

php - 如何使用 PHP 7 启用 MySQL

php - 选择最后插入的记录并分组mysql

php - laravel firstOrCreate 无法正常工作?

laravel - 将 Eloquent 模型限制为特定列

php - 急切加载模型中的附加属性

PHP array_count_values 返回一个空数组

php - 谷歌云消息错误 500(内部服务器错误)

php - Laravel中如何将不同表中的两个整数值相减

wpf - 资源字典没有延迟加载