1 <?php namespace BookStack\Entities;
3 use Illuminate\Database\Eloquent\Builder;
4 use Illuminate\Database\Eloquent\Relations\BelongsTo;
8 * @property int $book_id
9 * @property int $priority
10 * @property Book $book
11 * @method Builder whereSlugs(string $bookSlug, string $childSlug)
13 class BookChild extends Entity
17 * Scope a query to find items where the the child has the given childSlug
18 * where its parent has the bookSlug.
20 public function scopeWhereSlugs(Builder $query, string $bookSlug, string $childSlug)
22 return $query->with('book')
23 ->whereHas('book', function (Builder $query) use ($bookSlug) {
24 $query->where('slug', '=', $bookSlug);
26 ->where('slug', '=', $childSlug);
30 * Get the book this page sits in.
33 public function book(): BelongsTo
35 return $this->belongsTo(Book::class);
39 * Change the book that this entity belongs to.
41 public function changeBook(int $newBookId): Entity
43 $this->book_id = $newBookId;
48 // Update related activity
49 $this->activity()->update(['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);