belongsToMany(Book::class, 'bookshelves_books', 'bookshelf_id', 'book_id') ->select(['entities.*', 'entity_container_data.*']) ->withPivot('order') ->orderBy('order', 'asc'); } /** * Related books that are visible to the current user. */ public function visibleBooks(): BelongsToMany { return $this->books()->scopes('visible'); } /** * Get the url for this bookshelf. */ public function getUrl(string $path = ''): string { return url('/shelves/' . implode('/', [urlencode($this->slug), trim($path, '/')])); } /** * Check if this shelf contains the given book. */ public function contains(Book $book): bool { return $this->books()->where('id', '=', $book->id)->count() > 0; } /** * Add a book to the end of this shelf. */ public function appendBook(Book $book): void { if ($this->contains($book)) { return; } $maxOrder = $this->books()->max('order'); $this->books()->attach($book->id, ['order' => $maxOrder + 1]); } public function coverInfo(): EntityCover { return new EntityCover($this); } public function cover(): BelongsTo { return $this->belongsTo(Image::class, 'image_id'); } }