/** * @param $id * @param $numberOfPosts * @return \Journal\Post */ public function getPosts($id, $numberOfPosts) { return Post::with(['author', 'tags'])->whereHas('tags', function ($query) use($id) { // set a query to fetch posts according to the tag ID to which // a tag is related to $query->where('id', '=', $id); })->where('active', '=', 1)->where('status', '=', 1)->orderBy('published_at', 'DESC')->paginate($numberOfPosts); }
/** * @param $string * @param $id * @return string */ public function validateSlug($string, $id = null) { // slugify $slug = Str::slug(strtolower($string)); // check if ID is set if (is_null($id)) { $count = count(Post::where('slug', 'LIKE', $slug . '%')->get()); // return the slug return $count > 0 ? "{$slug}-{$count}" : $slug; } // get the post $post = $this->findById($id); // check if slug is the same with the user slug if ($post && $post->slug == $slug) { return $post->slug; } return $slug; }