/** * Get Discovered Tags * * @param TagRepositoryInterface $tagRepo * @param null $user * @return mixed */ public function discovered(TagRepositoryInterface $tagRepo, $user = null) { // Get recent tags $tags = $tagRepo->recent(10)->lists('name'); // Get items for recent tags $query = Item::with('itemable')->whereHas('tags', function ($query) use($tags) { $query->whereIn('name', $tags); })->where('itemable_type', 'App\\Models\\Link')->orderBy('created_at', 'desc'); // If we have a user, exclude their tags if ($user) { $query->where('user_id', '<>', $user->id); } return $query->simplePaginate(); }
/** * Gets Tag objects from cache or DB * @param CacheHandlerInterface $cacheHandler * @param TagRepositoryInterface $tagRepo * @return array */ public function getTagsForUser(CacheHandlerInterface $cacheHandler, TagRepositoryInterface $tagRepo) { // Check to see if there is a cache item $tags = $cacheHandler->get(CacheHandlerInterface::TAGS); // No tags found in cache, check DB, cache them, and return them if (!$tags) { $tags = $tagRepo->all(Auth::user()); $tagNames = []; foreach ($tags as $tag) { $tagNames[] = $tag->name; } $cacheHandler->set(CacheHandlerInterface::TAGS, $tagNames); return $tagNames; } return $tags; }