/** * Search posts for tag * @param string $tag * @return Response */ public function searchTag($tag) { try { $posts = Post::whereHas('tags', function ($q) use($tag) { $q->where('tag', $tag); })->where('seen', 1)->where('is_active', 1)->orderBy('created_at', 'desc')->paginate($this->itemPerPage); return view('website.index', compact('posts')); } catch (Exception $e) { $message = "Không tồn tại bài viết nào cho tag"; $alertClass = "alert-danger"; return redirect(route('website.index'))->with(compact('message', 'alertClass')); } }
public function listByTag($tag) { $model = Post::whereHas('tags', function ($q) use($tag) { $q->where('name', 'LIKE', "%" . $tag . "%"); })->with('tags')->get(); if ($model->count()) { $output = ['error' => false, 'count' => $model->count(), 'message' => 'Found many posts!', 'data' => $model->toArray()]; } else { $output = ['error' => true, 'message' => 'Not found any post belong to "' . $tag . '"', 'count' => 0]; } return response()->json($output, 200); }