you have an infinite loop A loads B which loads C which loads A.
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
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);
}
}
Please or to participate in this conversation.