]> BookStack Code Mirror - bookstack/blobdiff - app/Entities/Bookshelf.php
Show bookshelves that a book belongs to on a book view
[bookstack] / app / Entities / Bookshelf.php
index db6685688b3bcd2e6c449338f7ac9e83eef58803..7ad2415ed938628725d88ece0f010638c6f6493c 100644 (file)
@@ -100,8 +100,22 @@ class Bookshelf extends Entity
      * @param Book $book
      * @return bool
      */
-    public function contains(Book $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.
+     * @param Book $book
+     */
+    public function appendBook(Book $book)
+    {
+       if ($this->contains($book)) {
+           return;
+       }
+
+       $maxOrder = $this->books()->max('order');
+       $this->books()->attach($book->id, ['order' => $maxOrder + 1]);
+    }
 }