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 string $book_slug
14 * @property Book $book
16 * @method Builder whereSlugs(string $bookSlug, string $childSlug)
18 abstract class BookChild extends Entity
20 protected static function boot()
24 // Load book slugs onto these models by default during query-time
25 static::addGlobalScope('book_slug', function (Builder $builder) {
26 $builder->addSelect(['book_slug' => function ($builder) {
27 $builder->select('slug')
29 ->whereColumn('books.id', '=', 'book_id');
35 * Scope a query to find items where the child has the given childSlug
36 * where its parent has the bookSlug.
38 public function scopeWhereSlugs(Builder $query, string $bookSlug, string $childSlug)
40 return $query->with('book')
41 ->whereHas('book', function (Builder $query) use ($bookSlug) {
42 $query->where('slug', '=', $bookSlug);
44 ->where('slug', '=', $childSlug);
48 * Get the book this page sits in.
50 public function book(): BelongsTo
52 return $this->belongsTo(Book::class)->withTrashed();
56 * Change the book that this entity belongs to.
58 public function changeBook(int $newBookId): Entity
60 $this->book_id = $newBookId;
65 // Update all child pages if a chapter
66 if ($this instanceof Chapter) {
67 foreach ($this->pages()->withTrashed()->get() as $page) {
68 $page->changeBook($newBookId);