public function getQuestionDetail() { $request = Request::capture(); $token = $request->input('token'); $questionId = $request->input('questionId'); $userId = AuthController::getUserIdByToken($token); if ($userId == null) { return Utility::response_format(Utility::RESPONSE_CODE_AUTH_ERROR, '', '认证失败'); } if ($questionId == null) { return Utility::response_format(Utility::RESPONSE_CODE_Error, '', 'questionId不能为空'); } $question = Question::select('id', 'title', 'content', 'publish_time', 'user_id')->where('id', $questionId)->first()->toArray(); // print_r($question); // 个人信息 $userInfo = UserInfo::select('user_name', 'head_pic')->where('user_id', $question['user_id'])->first()->toArray(); $question = array_merge($question, $userInfo); // 图片 $picIds = QuestionPictures::select('pic_id')->where('question_id', $question['id'])->get()->toArray(); $pics = Picture::whereIn('id', $picIds)->get()->toArray(); $question = array_merge($question, ['image' => $pics]); // print_r($question); // 标签 $tagIds = QuestionTags::select('tag_id')->where('question_id', $question['id'])->get()->toArray(); $tags = Tags::whereIn('id', $tagIds)->get()->toArray(); $question = array_merge($question, ['tags' => $tags]); // 回答 时间倒序 前三条 $answers = Answer::select('*')->where('question_id', $questionId)->orderBy('answer_time', 'desc')->paginate(3, '*', 'page', 1)->toArray(); $result = array(); foreach ($answers['data'] as $answer) { // 个人信息 $userInfo = UserInfo::select('user_name', 'head_pic')->where('user_id', $answer['user_id'])->first()->toArray(); $answer = array_merge($answer, $userInfo); // 回答的评论数 $comments = Comment::select('*')->where('answer_id', $answer['id'])->get()->toArray(); $answer = array_merge($answer, ['commentNumber' => count($comments)]); array_push($result, $answer); } $question = array_merge($question, ['answers' => $result]); // 回答 最热门 三条 // .... return Utility::response_format(Utility::RESPONSE_CODE_SUCCESS, $question, '请求成功'); }
public function getTimeCmtById($id) { return Comment::select('time')->where('id', $id)->get()[0]['time']; }