コード例 #1
0
 /**
  * Save user's answer to a question.
  * Create a new record in user_answers table 
  *  if this is the first time this user answers this question,
  * Otherwise update the user's answer.
  *
  * @param Request $request
  * @return 1 on success, 0 on failure.
  */
 public function save(Request $request)
 {
     $userAnswer = UserAnswer::where(['user_id' => $request->user()->id, 'question_id' => $request->questionId])->first();
     if ($userAnswer) {
         $userAnswer->answer_id = $request->answerId;
     } else {
         $userAnswer = new UserAnswer(['user_id' => $request->user()->id, 'question_id' => $request->questionId, 'answer_id' => $request->answerId]);
     }
     if ($userAnswer->save()) {
         return 1;
     } else {
         return 0;
     }
 }
コード例 #2
0
 /**
  * Get user's answer to current question
  */
 public function userAnswer()
 {
     return $userAnswer = UserAnswer::where(['user_id' => Auth::user()->id, 'question_id' => $this->id])->first();
 }