1 <?php namespace Oxbow\Repos;
4 use Illuminate\Support\Str;
13 * ChapterRepo constructor.
16 public function __construct(Chapter $chapter)
18 $this->chapter = $chapter;
21 public function getById($id)
23 return $this->chapter->findOrFail($id);
26 public function getAll()
28 return $this->chapter->all();
31 public function getBySlug($slug, $bookId)
33 return $this->chapter->where('slug', '=', $slug)->where('book_id', '=', $bookId)->first();
36 public function newFromInput($input)
38 return $this->chapter->fill($input);
41 public function destroyById($id)
43 $page = $this->getById($id);
47 public function doesSlugExist($slug, $bookId, $currentId = false)
49 $query = $this->chapter->where('slug', '=', $slug)->where('book_id', '=', $bookId);
51 $query = $query->where('id', '!=', $currentId);
53 return $query->count() > 0;
56 public function findSuitableSlug($name, $bookId, $currentId = false)
58 $slug = Str::slug($name);
59 while($this->doesSlugExist($slug, $bookId, $currentId)) {
60 $slug .= '-' . substr(md5(rand(1, 500)), 0, 3);