- $terms = explode(' ', preg_quote(trim($term)));
- $chapters = $this->chapter->fullTextSearch(['name', 'description'], $terms, $whereTerms);
- $words = join('|', $terms);
+ $lastPage = $chapter->pages->last();
+ return $lastPage !== null ? $lastPage->priority + 1 : 0;
+ }
+
+ /**
+ * Get chapters by the given search term.
+ * @param string $term
+ * @param array $whereTerms
+ * @param int $count
+ * @param array $paginationAppends
+ * @return mixed
+ */
+ public function getBySearch($term, $whereTerms = [], $count = 20, $paginationAppends = [])
+ {
+ $terms = $this->prepareSearchTerms($term);
+ $chapterQuery = $this->permissionService->enforceChapterRestrictions($this->chapter->fullTextSearchQuery(['name', 'description'], $terms, $whereTerms));
+ $chapterQuery = $this->addAdvancedSearchQueries($chapterQuery, $term);
+ $chapters = $chapterQuery->paginate($count)->appends($paginationAppends);
+ $words = join('|', explode(' ', preg_quote(trim($term), '/')));