1 <?php namespace BookStack\Entities\Models;
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 abstract 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.
32 public function book(): BelongsTo
34 return $this->belongsTo(Book::class)->withTrashed();
38 * Change the book that this entity belongs to.
40 public function changeBook(int $newBookId): Entity
42 $this->book_id = $newBookId;
47 // Update all child pages if a chapter
48 if ($this instanceof Chapter) {
49 foreach ($this->pages()->withTrashed()->get() as $page) {
50 $page->changeBook($newBookId);