3 namespace BookStack\Entities\Queries;
5 use BookStack\Entities\Models\Chapter;
6 use BookStack\Exceptions\NotFoundException;
7 use Illuminate\Database\Eloquent\Builder;
9 class ChapterQueries implements ProvidesEntityQueries
11 protected static array $listAttributes = [
12 'id', 'slug', 'name', 'description', 'priority',
13 'created_at', 'updated_at',
14 'created_by', 'updated_by', 'owned_by',
17 public function start(): Builder
19 return Chapter::query();
22 public function findVisibleById(int $id): ?Chapter
24 return $this->start()->scopes('visible')->find($id);
27 public function findVisibleByIdOrFail(int $id): Chapter
29 return $this->start()->scopes('visible')->findOrFail($id);
32 public function findVisibleBySlugsOrFail(string $bookSlug, string $chapterSlug): Chapter
34 /** @var ?Chapter $chapter */
35 $chapter = $this->start()
38 ->whereHas('book', function (Builder $query) use ($bookSlug) {
39 $query->where('slug', '=', $bookSlug);
41 ->where('slug', '=', $chapterSlug)
44 if (is_null($chapter)) {
45 throw new NotFoundException(trans('errors.chapter_not_found'));
51 public function usingSlugs(string $bookSlug, string $chapterSlug): Builder
54 ->where('slug', '=', $chapterSlug)
55 ->whereHas('book', function (Builder $query) use ($bookSlug) {
56 $query->where('slug', '=', $bookSlug);
60 public function visibleForList(): Builder
64 ->select(array_merge(static::$listAttributes, ['book_slug' => function ($builder) {
65 $builder->select('slug')
67 ->whereColumn('books.id', '=', 'chapters.book_id');