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