Exemplo n.º 1
0
 /**
  * Display a listing of the resource.
  *
  * @param \App\Http\Requests\FilterArticlesRequest $request
  * @param string|null                              $slug
  * @return \Illuminate\Http\Response
  */
 public function index(FilterArticlesRequest $request, $slug = null)
 {
     $query = $slug ? Tag::whereSlug($slug)->firstOrFail()->articles() : new Article();
     // If you are relying on 'file' or 'database' cache, cacheTags() methods is not available
     $query = taggable() ? $query->with('comments', 'author', 'tags', 'solution', 'attachments')->remember(5)->cacheTags('articles') : $query->with('comments', 'author', 'tags', 'solution', 'attachments')->remember(5);
     $articles = $this->filter($request, $query)->paginate(10);
     return view('articles.index', compact('articles'));
 }
Exemplo n.º 2
0
 /**
  * Find all tricks for the tag that matches the given slug.
  *
  * @param string $slug
  * @param int    $perPage
  *
  * @return \Illuminate\Pagination\LengthAwarePaginator|\App\Trick[]
  */
 public function findByTag($slug, $perPage = self::PAGE_SIZE)
 {
     $tag = $this->tag->whereSlug($slug)->first();
     if (is_null($tag)) {
         throw new TagNotFoundException('The tag "' . $slug . '" does not exist!');
     }
     $tricks = $tag->tricks()->orderBy('created_at', 'desc')->paginate($perPage);
     return [$tag, $tricks];
 }
Exemplo n.º 3
0
 /**
  * Display a listing of the resource.
  *
  * @param \App\Http\Requests\FilterArticlesRequest $request
  * @param string|null                              $slug
  * @return \Illuminate\Http\Response
  */
 public function index(FilterArticlesRequest $request, $slug = null)
 {
     $query = $slug ? Tag::whereSlug($slug)->firstOrFail()->articles() : new Article();
     $cacheKey = cache_key('articles.index');
     $query = $this->filter($query->orderBy('pin', 'desc'));
     $args = $request->input(config('project.params.limit'), 5);
     $articles = $this->cache($cacheKey, 5, $query, 'paginate', $args);
     return $this->respondCollection($articles, $cacheKey);
 }
Exemplo n.º 4
0
 public function syncTags(Article $article, $tags = array(), $isNew = false)
 {
     $tagall = Tag::all()->toArray();
     $newTagIds = $updateTagIds = $existingSlug = $existingTag = [];
     if (!empty($tagall)) {
         foreach ($tagall as $tag) {
             $existingSlug[] = $tag['slug'];
             $existingTag[$tag['slug']] = $tag;
         }
     }
     unset($tagall);
     if ($tags) {
         foreach ($tags as $tag) {
             if (!in_array(mb_strtolower($tag, 'UTF-8'), $existingSlug)) {
                 $name = filter_allowed_words($tag);
                 $slug = preg_replace('/\\s+/', '-', mb_strtolower($name, 'UTF-8'));
                 if (in_array($slug, $existingSlug)) {
                     if ($isNew) {
                         Tag::whereSlug($slug)->increment('count');
                     }
                     $updateTagIds[] = $existingTag[$slug]['id'];
                 } else {
                     $firstLetter = getFirstLetter($name);
                     $newtag = Tag::create(array('name' => $name, 'slug' => $slug, 'letter' => $firstLetter));
                     $newId = $newtag->id;
                     $newTagIds[] = $newId;
                     $updateTagIds[] = $newId;
                 }
             } else {
                 if ($isNew) {
                     Tag::whereSlug($tag)->increment('count');
                 }
                 $updateTagIds[] = $existingTag[$tag]['id'];
             }
         }
     }
     $updateTagIds = array_unique($updateTagIds);
     if (!$isNew) {
         $oldTagIds = $article->tags->lists('id')->toArray();
         $delTagIds = array_diff($oldTagIds, $updateTagIds);
         $addTagIds = array_diff($updateTagIds, $oldTagIds);
         if (!empty($delTagIds)) {
             Tag::whereIn('id', $delTagIds)->decrement('count');
         }
         if (!empty($addTagIds)) {
             foreach ($addTagIds as $addId) {
                 if (!in_array($addId, $newTagIds)) {
                     Tag::whereId($addId)->increment('count');
                 }
             }
         }
     }
     $article->tags()->sync($updateTagIds);
     unset($newTagIds, $updateTagIds, $existingSlug, $existingTag);
 }
 /**
  * Display the specified resource.
  *
  * @param  int  $id
  * @return Response
  */
 public function show($slug)
 {
     $page_size = env('num_per_page');
     //        $articles = Tag::findBySlug($slug)->articles()->latest()->paginate($page_size);
     $articles = \App\Article::with('tags', 'category')->published()->whereHas('tags', function ($query) use($slug) {
         $query->whereSlug($slug);
     })->latest()->paginate($page_size);
     $Tag = Tag::whereSlug($slug)->firstOrFail();
     $Tagname = $Tag->name;
     return view('blog.tags.show', compact('articles', 'Tagname'));
 }
Exemplo n.º 6
0
 /**
  * Define your route model bindings, pattern filters, etc.
  *
  * @param  \Illuminate\Routing\Router  $router
  * @return void
  */
 public function boot(Router $router)
 {
     //
     parent::boot($router);
     // $router->model('articles', 'App\Article');
     $router->bind('articles', function ($slug) {
         return \App\Article::published()->whereSlug($slug)->firstOrFail();
     });
     $router->bind('tags', function ($slug) {
         return \App\Tag::whereSlug($slug)->firstOrFail();
     });
     $router->bind('users', function ($slug) {
         return \App\User::whereSlug($slug)->firstOrFail();
     });
 }
Exemplo n.º 7
0
 public function show($slug)
 {
     $tag = Tag::whereSlug($slug)->firstOrFail();
     $articles = $tag->articles()->orderBy('created_at', 'desc')->simplePaginate(10);
     return view('articles.tag', compact('tag', 'articles'));
 }
Exemplo n.º 8
0
 public function show($slug)
 {
     $tag = Tag::whereSlug($slug)->firstOrFail();
     $articles = $tag->articles()->latest()->simplePaginate(10);
     return view('articles.tag', compact('tag', 'articles'));
 }
Exemplo n.º 9
0
 public static function setTags($post, $tags = null)
 {
     $post->tags()->detach();
     $post_tags = [];
     if (isset($tags) && !empty($tags)) {
         foreach ($tags as $key => $value) {
             $current_tag = Tag::whereSlug(str_slug($value))->first();
             if (!$current_tag) {
                 $new_tag = Tag::create(['name' => $value, 'slug' => str_slug($value)]);
                 $add_tag_id = $new_tag->id;
             } else {
                 $add_tag_id = $current_tag->id;
             }
             array_push($post_tags, $add_tag_id);
         }
         $post->tags()->attach($post_tags);
     }
 }