3 namespace BookStack\Entities\Models;
5 use BookStack\References\ReferenceUpdater;
6 use Illuminate\Database\Eloquent\Builder;
7 use Illuminate\Database\Eloquent\Relations\BelongsTo;
12 * @property int $book_id
13 * @property int $priority
14 * @property string $book_slug
15 * @property Book $book
17 * @method Builder whereSlugs(string $bookSlug, string $childSlug)
19 abstract class BookChild extends Entity
22 * Scope a query to find items where the child has the given childSlug
23 * where its parent has the bookSlug.
25 public function scopeWhereSlugs(Builder $query, string $bookSlug, string $childSlug)
27 return $query->with('book')
28 ->whereHas('book', function (Builder $query) use ($bookSlug) {
29 $query->where('slug', '=', $bookSlug);
31 ->where('slug', '=', $childSlug);
35 * Get the book this page sits in.
37 public function book(): BelongsTo
39 return $this->belongsTo(Book::class)->withTrashed();
43 * Change the book that this entity belongs to.
45 public function changeBook(int $newBookId): Entity
47 $oldUrl = $this->getUrl();
48 $this->book_id = $newBookId;
53 if ($oldUrl !== $this->getUrl()) {
54 app()->make(ReferenceUpdater::class)->updateEntityReferences($this, $oldUrl);
57 // Update all child pages if a chapter
58 if ($this instanceof Chapter) {
59 foreach ($this->pages()->withTrashed()->get() as $page) {
60 $page->changeBook($newBookId);