3 namespace BookStack\Entities\Models;
5 use Illuminate\Database\Eloquent\Factories\HasFactory;
6 use Illuminate\Database\Eloquent\Relations\HasMany;
7 use Illuminate\Support\Collection;
12 * @property Collection<Page> $pages
13 * @property string $description
15 class Chapter extends BookChild
19 public $searchFactor = 1.2;
21 protected $fillable = ['name', 'description', 'priority'];
22 protected $hidden = ['pivot', 'deleted_at'];
25 * Get the pages that this chapter contains.
27 * @return HasMany<Page>
29 public function pages(string $dir = 'ASC'): HasMany
31 return $this->hasMany(Page::class)->orderBy('priority', $dir);
35 * Get the url of this chapter.
37 public function getUrl(string $path = ''): string
41 urlencode($this->book_slug ?? $this->book->slug),
43 urlencode($this->slug),
47 return url('/' . implode('/', $parts));
51 * Get the visible pages in this chapter.
53 public function getVisiblePages(): Collection
57 ->orderBy('draft', 'desc')
58 ->orderBy('priority', 'asc')
63 * Get a visible chapter by its book and page slugs.
64 * @throws \Illuminate\Database\Eloquent\ModelNotFoundException
66 public static function getBySlugs(string $bookSlug, string $chapterSlug): self
68 return static::visible()->whereSlugs($bookSlug, $chapterSlug)->firstOrFail();