示例#1
0
 public function postComment(Request $request, $news_id)
 {
     $this->validate($request, ['type' => 'in:agree,disagree', 'content' => 'required|between:1,100']);
     $news = News::findOrFail($news_id);
     $comment = new Comment(['type' => $request->type, 'content' => $request->content, 'user_id' => Auth::user()->id]);
     $news->comments()->save($comment);
     return json_encode(['comments' => $this->comment->forNews($news_id)]);
 }
示例#2
0
 public function news($news_id)
 {
     $user_id = 0;
     if (Auth::check()) {
         $user_id = Auth::user()->id;
     }
     $script = "<script>\n\t\tvar news_id = {$news_id};\n\t\tvar user_id={$user_id};\n\t</script>";
     $news = News::findOrFail($news_id);
     try {
         DB::beginTransaction();
         $news->increment("visit");
         $news->save();
         DB::commit();
     } catch (Exception $exception) {
         DB::rollback();
     }
     return view('pages.news', ['ngController' => 'NewsController', 'title' => $news->title, 'news' => $news->parse(), 'script' => $script]);
 }
 public function postDelete(Request $request, $id)
 {
     $news = News::findOrFail($id);
     $news->delete();
     return redirect()->action('NewsController@getIndex');
 }
 public function getComments(Request $request, $newsId)
 {
     $news = News::findOrFail($newsId);
     $querys = $request->query();
     $comments = $news->getComments()->orderBy('created_at', 'desc');
     if (isset($querys['author_id']) && is_numeric($querys['author_id'])) {
         $author_id = (int) $querys['author_id'];
         $comments = $comments->where('author_id', $author_id);
     }
     $page = 1;
     if (isset($querys['page']) && is_numeric($querys['page'])) {
         $page = (int) $querys['page'];
     }
     $pageSize = 20;
     if (isset($querys['per_page']) && is_numeric($querys['per_page'])) {
         $pageSize = (int) $querys['per_page'];
     }
     $totalCount = count($comments->get());
     $skip = $pageSize * ($page - 1);
     $comments = $comments->skip($skip)->take($pageSize)->get();
     foreach ($comments as $comment) {
         $comment = $this->unfoldCommentInfo($comment);
     }
     $result = array('page' => $page, 'per_page' => $pageSize, 'total_count' => $totalCount, 'data' => $comments);
     return response()->json($result);
 }
示例#5
0
 public function getTags($id)
 {
     $news = News::findOrFail($id);
     return json_encode($news->Tags());
 }
示例#6
0
 /**
  * Display the specified News.
  *
  * @param  int  $id
  * @return Response
  */
 public function show($id)
 {
     $active = 'news';
     $news = News::findOrFail($id);
     return view('frontend.news.show', compact('active', 'news'));
 }