Laravel eager loading 4 tables

Viewed 160

I have this problem with 'eager loading' in laravel.

Im working with 4 tables that are related to each other.

I have these modals:

<?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';
}

?>

Now i want to 'get' all the data in one object.

When i do this:

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

It works PERFECT

but when i do this:

$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)
2 Answers
Related