3 namespace BookStack\Entities\Models;
5 use BookStack\References\ReferenceUpdater;
6 use Illuminate\Database\Eloquent\Relations\BelongsTo;
11 * @property int $book_id
12 * @property int $priority
13 * @property string $book_slug
14 * @property Book $book
16 abstract class BookChild extends Entity
19 * Get the book this page sits in.
21 public function book(): BelongsTo
23 return $this->belongsTo(Book::class)->withTrashed();
27 * Change the book that this entity belongs to.
29 public function changeBook(int $newBookId): self
31 $oldUrl = $this->getUrl();
32 $this->book_id = $newBookId;
33 $this->unsetRelation('book');
37 if ($oldUrl !== $this->getUrl()) {
38 app()->make(ReferenceUpdater::class)->updateEntityReferences($this, $oldUrl);
41 // Update all child pages if a chapter
42 if ($this instanceof Chapter) {
43 foreach ($this->pages()->withTrashed()->get() as $page) {
44 $page->changeBook($newBookId);