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 findVisibleBySlugsOrFail(string $bookSlug, string $chapterSlug): Chapter
29 /** @var ?Chapter $chapter */
30 $chapter = $this->start()->with('book')
31 ->whereHas('book', function (Builder $query) use ($bookSlug) {
32 $query->where('slug', '=', $bookSlug);
34 ->where('slug', '=', $chapterSlug)
37 if (is_null($chapter)) {
38 throw new NotFoundException(trans('errors.chapter_not_found'));
44 public function visibleForList(): Builder
47 ->select(array_merge(static::$listAttributes, ['book_slug' => function ($builder) {
48 $builder->select('slug')
50 ->whereColumn('books.id', '=', 'chapters.book_id');