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 idExists($id)
23 return $this->chapter->where('id', '=', $id)->count() > 0;
26 public function getById($id)
28 return $this->chapter->findOrFail($id);
31 public function getAll()
33 return $this->chapter->all();
36 public function getBySlug($slug, $bookId)
38 return $this->chapter->where('slug', '=', $slug)->where('book_id', '=', $bookId)->first();
41 public function newFromInput($input)
43 return $this->chapter->fill($input);
46 public function destroyById($id)
48 $page = $this->getById($id);
52 public function doesSlugExist($slug, $bookId, $currentId = false)
54 $query = $this->chapter->where('slug', '=', $slug)->where('book_id', '=', $bookId);
56 $query = $query->where('id', '!=', $currentId);
58 return $query->count() > 0;
61 public function findSuitableSlug($name, $bookId, $currentId = false)
63 $slug = Str::slug($name);
64 while($this->doesSlugExist($slug, $bookId, $currentId)) {
65 $slug .= '-' . substr(md5(rand(1, 500)), 0, 3);