diff --git a/src/Concerns/AutoloadsRelationships.php b/src/Concerns/AutoloadsRelationships.php index 125d4ff..afb3091 100644 --- a/src/Concerns/AutoloadsRelationships.php +++ b/src/Concerns/AutoloadsRelationships.php @@ -24,7 +24,7 @@ trait AutoloadsRelationships /** * @var ?Collection */ - protected $parentCollection = null; + public $parentCollection = null; /** * @var ?LoggerInterface @@ -112,7 +112,9 @@ private function logAutoload(string $relationship) */ public function getRelationshipFromMethod($method) { + $fqRelation = ""; $relation = $this->$method(); + $fqRelation = $method; if (!$relation instanceof Relation) { throw new LogicException( @@ -122,9 +124,17 @@ public function getRelationshipFromMethod($method) if ($this->shouldAutoLoad()) { if (!$this->relationLoaded($method)) { - $this->logAutoload($method); - $this->parentCollection->load($method); + $parentCollection = $this->parentCollection; + while (isset($parentCollection->caller)) { + $fqRelation = $parentCollection->caller->method . "." . $fqRelation; + $parentCollection = $parentCollection->caller->model->parentCollection; + } + $this->logAutoload($fqRelation); + $parentCollection->loadMissing($fqRelation); + + $collection = $this->relations[$method]; + $collection->caller = (object) ['method' => $method, 'model' => $this]; return $this->relations[$method]; } }