X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/8e78b4c43eb980f47d9c207a0ce3330699d54103..refs/pull/5725/head:/app/Entities/Queries/ChapterQueries.php diff --git a/app/Entities/Queries/ChapterQueries.php b/app/Entities/Queries/ChapterQueries.php index dcfc4aad3..53c5bc9d8 100644 --- a/app/Entities/Queries/ChapterQueries.php +++ b/app/Entities/Queries/ChapterQueries.php @@ -10,8 +10,7 @@ class ChapterQueries implements ProvidesEntityQueries { protected static array $listAttributes = [ 'id', 'slug', 'name', 'description', 'priority', - 'created_at', 'updated_at', - 'created_by', 'updated_by', 'owned_by', + 'book_id', 'created_at', 'updated_at', 'owned_by', ]; public function start(): Builder @@ -24,26 +23,43 @@ class ChapterQueries implements ProvidesEntityQueries return $this->start()->scopes('visible')->find($id); } - public function findVisibleBySlugs(string $bookSlug, string $chapterSlug): Chapter + public function findVisibleByIdOrFail(int $id): Chapter + { + return $this->start()->scopes('visible')->findOrFail($id); + } + + public function findVisibleBySlugsOrFail(string $bookSlug, string $chapterSlug): Chapter { /** @var ?Chapter $chapter */ - $chapter = $this->start()->with('book') + $chapter = $this->start() + ->scopes('visible') + ->with('book') ->whereHas('book', function (Builder $query) use ($bookSlug) { $query->where('slug', '=', $bookSlug); }) ->where('slug', '=', $chapterSlug) ->first(); - if ($chapter === null) { + if (is_null($chapter)) { throw new NotFoundException(trans('errors.chapter_not_found')); } return $chapter; } + public function usingSlugs(string $bookSlug, string $chapterSlug): Builder + { + return $this->start() + ->where('slug', '=', $chapterSlug) + ->whereHas('book', function (Builder $query) use ($bookSlug) { + $query->where('slug', '=', $bookSlug); + }); + } + public function visibleForList(): Builder { return $this->start() + ->scopes('visible') ->select(array_merge(static::$listAttributes, ['book_slug' => function ($builder) { $builder->select('slug') ->from('books')