getAttribute($relationName . '_type'); $id = $relation->getAttribute($relationName . '_id'); if (!isset($idsByType[$type])) { $idsByType[$type] = []; } $idsByType[$type][] = $id; } $modelMap = $this->idsByTypeToModelMap($idsByType, $loadParents); foreach ($relations as $relation) { $type = $relation->getAttribute($relationName . '_type'); $id = $relation->getAttribute($relationName . '_id'); $related = $modelMap[$type][strval($id)] ?? null; if ($related) { $relation->setRelation($relationName, $related); } } } /** * @param array $idsByType * @return array> */ protected function idsByTypeToModelMap(array $idsByType, bool $eagerLoadParents): array { $modelMap = []; foreach ($idsByType as $type => $ids) { $models = $this->queries->visibleForList($type) ->whereIn('id', $ids) ->with($eagerLoadParents ? $this->getRelationsToEagerLoad($type) : []) ->get(); if (count($models) > 0) { $modelMap[$type] = []; } foreach ($models as $model) { $modelMap[$type][strval($model->id)] = $model; } } return $modelMap; } protected function getRelationsToEagerLoad(string $type): array { $toLoad = []; $loadVisible = fn (Relation $query) => $query->scopes('visible'); if ($type === 'chapter' || $type === 'page') { $toLoad['book'] = $loadVisible; } if ($type === 'page') { $toLoad['chapter'] = $loadVisible; } return $toLoad; } }