*/
public function format(): array
{
+ if ($this->includeRelatedTitles) {
+ $this->loadRelatedTitles();
+ }
+
$results = [];
foreach ($this->list as $item) {
return $results;
}
+ /**
+ * Eager load the related book and chapter data when needed.
+ */
+ protected function loadRelatedTitles(): void
+ {
+ $pages = collect($this->list)->filter(fn($item) => $item instanceof Page);
+
+ foreach ($this->list as $entity) {
+ if (method_exists($entity, 'book')) {
+ $entity->load('book');
+ }
+ if ($entity instanceof Page && $entity->chapter_id) {
+ $entity->load('chapter');
+ }
+ }
+ }
+
/**
* Format a single entity item to a plain array.
*/