+ $contents = (new BookContents($book))->getTree(true, false)->all();
+ $contentsApiData = (new ApiEntityListFormatter($contents))
+ ->withType()
+ ->withField('pages', function (Entity $entity) {
+ if ($entity instanceof Chapter) {
+ return (new ApiEntityListFormatter($entity->pages->all()))->format();
+ }
+ return null;
+ })->format();
+ $book->setAttribute('contents', $contentsApiData);
+