- return $this->chapter->where('slug', '=', $slug)->where('book_id', '=', $bookId)->first();
+ $chapter = $this->chapterQuery()->where('slug', '=', $slug)->where('book_id', '=', $bookId)->first();
+ if ($chapter === null) throw new NotFoundException('Chapter not found');
+ return $chapter;
+ }
+
+ /**
+ * Get the child items for a chapter
+ * @param Chapter $chapter
+ */
+ public function getChildren(Chapter $chapter)
+ {
+ $pages = $this->permissionService->enforcePageRestrictions($chapter->pages())->get();
+ // Sort items with drafts first then by priority.
+ return $pages->sortBy(function ($child, $key) {
+ $score = $child->priority;
+ if ($child->draft) $score -= 100;
+ return $score;
+ });