1 <?php namespace BookStack;
4 class Chapter extends Entity
6 protected $fillable = ['name', 'description', 'priority', 'book_id'];
9 * Get the book this chapter is within.
10 * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
12 public function book()
14 return $this->belongsTo(Book::class);
18 * Get the pages that this chapter contains.
21 public function pages()
23 return $this->hasMany(Page::class)->orderBy('priority', 'ASC');
27 * Get the url of this chapter.
28 * @param string|bool $path
31 public function getUrl($path = false)
33 $bookSlug = $this->getAttribute('bookSlug') ? $this->getAttribute('bookSlug') : $this->book->slug;
34 if ($path !== false) {
35 return baseUrl('/books/' . urlencode($bookSlug) . '/chapter/' . urlencode($this->slug) . '/' . trim($path, '/'));
37 return baseUrl('/books/' . urlencode($bookSlug) . '/chapter/' . urlencode($this->slug));
41 * Get an excerpt of this chapter's description to the specified length or less.
45 public function getExcerpt($length = 100)
47 $description = $this->description;
48 return strlen($description) > $length ? substr($description, 0, $length-3) . '...' : $description;