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
18 use HasHtmlDescription;
20 public float $searchFactor = 1.2;
22 protected $fillable = ['name', 'description', 'priority'];
23 protected $hidden = ['pivot', 'deleted_at'];
26 * Get the pages that this chapter contains.
28 * @return HasMany<Page>
30 public function pages(string $dir = 'ASC'): HasMany
32 return $this->hasMany(Page::class)->orderBy('priority', $dir);
36 * Get the url of this chapter.
38 public function getUrl(string $path = ''): string
42 urlencode($this->book_slug ?? $this->book->slug),
44 urlencode($this->slug),
48 return url('/' . implode('/', $parts));
52 * Get the visible pages in this chapter.
54 public function getVisiblePages(): Collection
58 ->orderBy('draft', 'desc')
59 ->orderBy('priority', 'asc')
64 * Get a visible chapter by its book and page slugs.
65 * @throws \Illuminate\Database\Eloquent\ModelNotFoundException
67 public static function getBySlugs(string $bookSlug, string $chapterSlug): self
69 return static::visible()->whereSlugs($bookSlug, $chapterSlug)->firstOrFail();