3 namespace BookStack\Entities\Models;
5 use Illuminate\Database\Eloquent\Builder;
6 use Illuminate\Database\Eloquent\Relations\BelongsTo;
11 * @property int $book_id
12 * @property int $priority
13 * @property Book $book
15 * @method Builder whereSlugs(string $bookSlug, string $childSlug)
17 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.
35 public function book(): BelongsTo
37 return $this->belongsTo(Book::class)->withTrashed();
41 * Change the book that this entity belongs to.
43 public function changeBook(int $newBookId): Entity
45 $this->book_id = $newBookId;
50 // Update all child pages if a chapter
51 if ($this instanceof Chapter) {
52 foreach ($this->pages()->withTrashed()->get() as $page) {
53 $page->changeBook($newBookId);