Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

itwasntme's avatar

Eager Load deep recursion problem

Hi there,

i've got a problem with the eloquent / laravel eager loading. I've got a nested function level reached. I'm searching for a stop or max level.

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class A extends Model
{
    protected $with = ['B'];
    
    public function B() {
        return $this->belongsTo('App\B');
    }
}

class B extends Model
{
    protected $with = ['C'];
    
    public function C() {
        return $this->belongsTo('App\C');
    }
}

class C extends Model
{
    protected $with = ['A'];
    
    public function A() {
        return $this->belongsTo('App\A');
    }
}


dd(\App\A::first());

--->PHP Fatal error: Maximum function nesting level of '512' reached, aborting! in /home/vagrant/Projects/portalconnector/vendor/laravel/framework/src/Illuminate/Foundation/Application.php on line 666

0 likes
5 replies
ahuggins's avatar

you have an infinite loop A loads B which loads C which loads A.

1 like
itwasntme's avatar

yes i know... but i'd like to stop it - I NEED auto eager loading within my project, but I also need some ideas to stop this stuff

ahuggins's avatar

Hmm, hard to say for sure since you are providing an abstract example. But you can Lazy Eager Load the relationships at the point they are being used instead of globally like you are in the example.

Lazy Eager Loading means you would get your collection of A, and then where that is used, load B and C after you have already retrieved the collection of A. It's not ideal, but it should prevent the issue you are having.

Ori's avatar

well... there is one solution, but it is not ideal one.


<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class A extends Model
{
//    protected $with = ['B'];  
    
    public function B() {
        return $this->belongsTo('App\B');
    }
}

class B extends Model
{
    protected $with = ['C'];
    
    public function C() {
        return $this->belongsTo('App\C');
    }
}

class C extends Model
{
    protected $with = ['A'];
    
    public function A() {
        return $this->belongsTo('App\A');
    }
}


dd(\App\A::with('B')->first());


I would rather rethink your app logic

NicksonYap's avatar
Level 1

There is a without() method (tested on Laravel 5.4) https://laravel.com/api/5.4/Illuminate/Database/Eloquent/Builder.html#method_without

Placing without() on both sides of a relationship worked.

Example on 2-way relationship: https://stackoverflow.com/a/52880179/3553367

I have not tested on this 3-way relationship but below is an example


<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class A extends Model
{
    protected $with = ['B'];
    
    public function B() {
        return $this->belongsTo('App\B')->without('A');
    }
}

class B extends Model
{
    protected $with = ['C'];
    
    public function C() {
        return $this->belongsTo('App\C')->without('B');
    }
}

class C extends Model
{
    protected $with = ['A'];
    
    public function A() {
        return $this->belongsTo('App\A')->without('C);
    }
}

3 likes

Please or to participate in this conversation.