public function addPraise() { $praise = PostsPraise::where('p_id', '=', $this->p_id)->where('u_id', '=', $this->u_id)->first(); if (isset($praise)) { throw new Exception("已经点过赞了", 1); } $this->save(); return true; }
/** * add/del praise * @author Kydz 2015-06-17 * @param [type] $id [description] * @return [type] [description] */ public function praise($id) { $token = Input::get('token'); $u_id = Input::get('u_id'); $type = Input::get('type'); $post = Post::find($id); try { $user = User::chkUserByToken($token, $u_id); $result = 2000; if ($type == 1) { $post->addPraise(); $praise = new PostsPraise(); $praise->p_id = $id; $praise->u_id = $user->u_id; $praise->created_at = date('Y-m-d H:i:s'); $praise->addPraise(); $info = '点赞成功'; } elseif ($type == 2) { $post->delPraise(); $praise = PostsPraise::where('p_id', '=', $id)->where('u_id', '=', $user->u_id)->first(); if (isset($praise->pp_id)) { $praise->delete(); } $info = '取消赞成功'; } else { $result = 2001; $info = '操作失败'; } $re = ['result' => $result, 'data' => [], 'info' => $info]; } catch (Exception $e) { $re = ['result' => 2001, 'data' => [], 'info' => $e->getMessage()]; } return Response::json($re); }