1 <?php namespace Oxbow\Repos;
4 use Illuminate\Support\Str;
12 * PageRepo constructor.
15 public function __construct(Page $page)
20 public function idExists($id)
22 return $this->page->where('page_id', '=', $id)->count() > 0;
25 public function getById($id)
27 return $this->page->findOrFail($id);
30 public function getAll()
32 return $this->page->all();
35 public function getBySlug($slug, $bookId)
37 return $this->page->where('slug', '=', $slug)->where('book_id', '=', $bookId)->first();
40 public function newFromInput($input)
42 $page = $this->page->fill($input);
46 public function countBySlug($slug, $bookId)
48 return $this->page->where('slug', '=', $slug)->where('book_id', '=', $bookId)->count();
51 public function destroyById($id)
53 $page = $this->getById($id);
57 public function getBySearch($term)
59 $terms = explode(' ', trim($term));
61 foreach($terms as $term) {
62 $query = $query->where('text', 'like', '%'.$term.'%');
68 * Checks if a slug exists within a book already.
71 * @param bool|false $currentId
74 public function doesSlugExist($slug, $bookId, $currentId = false)
76 $query = $this->page->where('slug', '=', $slug)->where('book_id', '=', $bookId);
78 $query = $query->where('id', '!=', $currentId);
80 return $query->count() > 0;
84 * Gets a suitable slug for the resource
88 * @param bool|false $currentId
91 public function findSuitableSlug($name, $bookId, $currentId = false)
93 $slug = Str::slug($name);
94 while($this->doesSlugExist($slug, $bookId, $currentId)) {
95 $slug .= '-' . substr(md5(rand(1, 500)), 0, 3);