+ /**
+ * Include parent book/chapter info in the formatted data.
+ */
+ public function withParents(): self
+ {
+ $this->withField('book', function (Entity $entity) {
+ if ($entity instanceof BookChild && $entity->book) {
+ return $entity->book->only(['id', 'name', 'slug']);
+ }
+ return null;
+ });
+
+ $this->withField('chapter', function (Entity $entity) {
+ if ($entity instanceof Page && $entity->chapter) {
+ return $entity->chapter->only(['id', 'name', 'slug']);
+ }
+ return null;
+ });
+
+ return $this;
+ }
+