chapters() public method

Get all chapters within this book.
public chapters ( ) : Illuminate\Database\Eloquent\Relations\HasMany
return Illuminate\Database\Eloquent\Relations\HasMany
示例#1
0
 /**
  * Get all child objects of a book.
  * Returns a sorted collection of Pages and Chapters.
  * Loads the book slug onto child elements to prevent access database access for getting the slug.
  * @param Book $book
  * @param bool $filterDrafts
  * @return mixed
  */
 public function getChildren(Book $book, $filterDrafts = false)
 {
     $pageQuery = $book->pages()->where('chapter_id', '=', 0);
     $pageQuery = $this->permissionService->enforcePageRestrictions($pageQuery, 'view');
     if ($filterDrafts) {
         $pageQuery = $pageQuery->where('draft', '=', false);
     }
     $pages = $pageQuery->get();
     $chapterQuery = $book->chapters()->with(['pages' => function ($query) use($filterDrafts) {
         $this->permissionService->enforcePageRestrictions($query, 'view');
         if ($filterDrafts) {
             $query->where('draft', '=', false);
         }
     }]);
     $chapterQuery = $this->permissionService->enforceChapterRestrictions($chapterQuery, 'view');
     $chapters = $chapterQuery->get();
     $children = $pages->values();
     foreach ($chapters as $chapter) {
         $children->push($chapter);
     }
     $bookSlug = $book->slug;
     $children->each(function ($child) use($bookSlug) {
         $child->setAttribute('bookSlug', $bookSlug);
         if ($child->isA('chapter')) {
             $child->pages->each(function ($page) use($bookSlug) {
                 $page->setAttribute('bookSlug', $bookSlug);
             });
             $child->pages = $child->pages->sortBy(function ($child, $key) {
                 $score = $child->priority;
                 if ($child->draft) {
                     $score -= 100;
                 }
                 return $score;
             });
         }
     });
     // Sort items with drafts first then by priority.
     return $children->sortBy(function ($child, $key) {
         $score = $child->priority;
         if ($child->isA('page') && $child->draft) {
             $score -= 100;
         }
         return $score;
     });
 }
示例#2
0
 /**
  * Get all child objects of a book.
  * Returns a sorted collection of Pages and Chapters.
  * Loads the bookslug onto child elements to prevent access database access for getting the slug.
  * @param Book $book
  * @return mixed
  */
 public function getChildren(Book $book)
 {
     $pages = $book->pages()->where('chapter_id', '=', 0)->get();
     $chapters = $book->chapters()->with('pages')->get();
     $children = $pages->merge($chapters);
     $bookSlug = $book->slug;
     $children->each(function ($child) use($bookSlug) {
         $child->setAttribute('bookSlug', $bookSlug);
         if ($child->isA('chapter')) {
             $child->pages->each(function ($page) use($bookSlug) {
                 $page->setAttribute('bookSlug', $bookSlug);
             });
         }
     });
     return $children->sortBy('priority');
 }
示例#3
0
 /**
  * Create a new chapter from request input.
  * @param $input
  * @param Book $book
  * @return Chapter
  */
 public function createFromInput($input, Book $book)
 {
     $chapter = $this->chapter->newInstance($input);
     $chapter->slug = $this->findSuitableSlug($chapter->name, $book->id);
     $chapter->created_by = user()->id;
     $chapter->updated_by = user()->id;
     $chapter = $book->chapters()->save($chapter);
     $this->permissionService->buildJointPermissionsForEntity($chapter);
     return $chapter;
 }