Example #1
0
 /**
  * Get all the replies correponding to question's id by an user.
  *	It's ajax function
  * @return Response
  */
 public function getAnswer(Request $request)
 {
     $statusCode = 200;
     $response = ['status' => false, 'msg' => ""];
     if ($request->input('questionid') != '') {
         //$answers = Answer::where("question_id", "=", $request->input('questionid'))->get();
         $answers = Answer::leftJoin('users', function ($join) {
             $join->on('answers.user_id', '=', 'users.id');
         })->where("question_id", "=", $request->input('questionid'))->get(['answers.id', 'answers.answer', 'answers.question_id', 'users.id', 'users.name']);
         if (isset($answers) && count($answers) > 0) {
             $html = '';
             $html .= '<h4>Replies</h4>';
             $i = 1;
             foreach ($answers as $answer) {
                 $html .= '<p>' . $i . '. ' . $answer->answer . '<span class="pull-right">Reply by: <a>' . $answer->name . '</a></span></p>';
                 $i++;
             }
             $response = ['status' => true, 'data' => $html, 'msg' => "Success"];
         } else {
             $response = ['status' => false, 'msg' => "No replies to this question."];
         }
     } else {
         $response = ['status' => false, 'msg' => "Question number is required"];
     }
     return response()->json($response, $statusCode);
 }