1 <?php namespace BookStack\Entities\Models;
3 use BookStack\Entities\Models\Chapter;
4 use BookStack\Entities\Models\Entity;
5 use BookStack\Entities\Models\Book;
6 use Illuminate\Database\Eloquent\Builder;
7 use Illuminate\Database\Eloquent\Relations\BelongsTo;
11 * @property int $book_id
12 * @property int $priority
13 * @property Book $book
14 * @method Builder whereSlugs(string $bookSlug, string $childSlug)
16 abstract class BookChild extends Entity
20 * Scope a query to find items where the the child has the given childSlug
21 * where its parent has the bookSlug.
23 public function scopeWhereSlugs(Builder $query, string $bookSlug, string $childSlug)
25 return $query->with('book')
26 ->whereHas('book', function (Builder $query) use ($bookSlug) {
27 $query->where('slug', '=', $bookSlug);
29 ->where('slug', '=', $childSlug);
33 * Get the book this page sits in.
36 public function book(): BelongsTo
38 return $this->belongsTo(Book::class);
42 * Change the book that this entity belongs to.
44 public function changeBook(int $newBookId): Entity
46 $this->book_id = $newBookId;
51 // Update all child pages if a chapter
52 if ($this instanceof Chapter) {
53 foreach ($this->pages as $page) {
54 $page->changeBook($newBookId);