3 namespace BookStack\Entities\Models;
5 use Illuminate\Database\Eloquent\Relations\BelongsTo;
6 use Illuminate\Database\Eloquent\Factories\HasFactory;
7 use Illuminate\Database\Eloquent\Relations\HasMany;
8 use Illuminate\Support\Collection;
13 * @property Collection<Page> $pages
14 * @property string $description
15 * @property ?int $default_template_id
16 * @property ?Page $defaultTemplate
18 class Chapter extends BookChild
21 use HasHtmlDescription;
23 public float $searchFactor = 1.2;
25 protected $fillable = ['name', 'description', 'priority'];
26 protected $hidden = ['pivot', 'deleted_at', 'description_html'];
29 * Get the pages that this chapter contains.
31 * @return HasMany<Page>
33 public function pages(string $dir = 'ASC'): HasMany
35 return $this->hasMany(Page::class)->orderBy('priority', $dir);
39 * Get the url of this chapter.
41 public function getUrl(string $path = ''): string
45 urlencode($this->book_slug ?? $this->book->slug),
47 urlencode($this->slug),
51 return url('/' . implode('/', $parts));
55 * Get the Page that is used as default template for newly created pages within this Chapter.
57 public function defaultTemplate(): BelongsTo
59 return $this->belongsTo(Page::class, 'default_template_id');
63 * Get the visible pages in this chapter.
65 public function getVisiblePages(): Collection
69 ->orderBy('draft', 'desc')
70 ->orderBy('priority', 'asc')
75 * Get a visible chapter by its book and page slugs.
76 * @throws \Illuminate\Database\Eloquent\ModelNotFoundException
78 public static function getBySlugs(string $bookSlug, string $chapterSlug): self
80 return static::visible()->whereSlugs($bookSlug, $chapterSlug)->firstOrFail();