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 ?int $default_template_id
15 * @property ?Page $defaultTemplate
17 class Chapter extends BookChild
20 use HasHtmlDescription;
22 public float $searchFactor = 1.2;
24 protected $fillable = ['name', 'description', 'priority'];
25 protected $hidden = ['pivot', 'deleted_at', 'description_html'];
28 * Get the pages that this chapter contains.
30 * @return HasMany<Page>
32 public function pages(string $dir = 'ASC'): HasMany
34 return $this->hasMany(Page::class)->orderBy('priority', $dir);
38 * Get the url of this chapter.
40 public function getUrl(string $path = ''): string
44 urlencode($this->book_slug ?? $this->book->slug),
46 urlencode($this->slug),
50 return url('/' . implode('/', $parts));
54 * Get the Page that is used as default template for newly created pages within this Chapter.
56 public function defaultTemplate(): BelongsTo
58 return $this->belongsTo(Page::class, 'default_template_id');
62 * Get the visible pages in this chapter.
63 * @returns Collection<Page>
65 public function getVisiblePages(): Collection
69 ->orderBy('draft', 'desc')
70 ->orderBy('priority', 'asc')