public function articleCollection() { if (!Sentry::check()) { return Response::json(array('errCode' => 10, 'message' => '请登录')); } // Sentry::login(Sentry::findUserById(5), false); $article_id = Input::get('article_id'); $article_focus = ArticleFocus::where('user_id', '=', Sentry::getUser()->id)->where('article_id', '=', $article_id)->first(); if (count($article_focus) == 1) { try { DB::transaction(function () use($article_id, $article_focus) { //添加收藏话题条数 $article = Article::find($article_id); $article->focus_num = $article->focus_num + 1; $article->save(); $article_focus = DB::table('article_focus')->where('user_id', '=', Sentry::getUser()->id)->where('article_id', '=', $article_id); $article_focus->delete(); }); } catch (\Exception $e) { return Response::json(array('errCode' => 11, 'message' => '操作失败')); } return Response::json(array('errCode' => 0, 'message' => '取消收藏成功!')); } else { try { DB::transaction(function () use($article_id) { //添加收藏话题条数 $article = Article::find($article_id); $article->focus_num = $article->focus_num - 1; $article->save(); $article_focus = new ArticleFocus(); $article_focus->user_id = Sentry::getUser()->id; $article_focus->article_id = $article_id; $article_focus->save(); }); } catch (\Exception $e) { return Response::json(array('errCode' => 11, 'message' => '操作失败')); } return Response::json(array('errCode' => 0, 'message' => '收藏成功!')); } }