/** * Display the specified resource. * * @param int $id * @return Response */ public function show($id) { $Q = Question::with('questionnaire')->find($id); if (is_null($Q)) { return Response::json(['error' => 'no such question found'], 404); } return Response::json($Q, 200); }
public function postAdminEditQuestions($question_id) { $question = Question::with('answers')->findOrFail($question_id); $question->text = Input::get('question'); $question->correct_answer_id = Input::get('correct_answer'); foreach ($question->answers as $answer) { $answer->text = Input::get('answers')[$answer->id]; $answer->save(); } if ($question->save()) { return Redirect::route('getAdminEditQuestions', $question_id)->with('success', 'You have edit question successfully.'); } else { return Redirect::route('getAdminEditQuestions', $question_id)->with('fail', 'Error. Please try again later.'); } }
public function questions() { if (Auth::user()->correct_answers_count < 3) { $result = Question::where('type', 0)->get(); } else { if (Auth::user()->correct_answers_count == 3) { $result = Question::where('type', 1)->get(); } else { return Redirect::to('confirme'); } } $rand = rand(0, count($result) - 1); $question = Question::with('answers')->find($result[$rand]->id); return View::make('user.questions')->with('question', $question); }
}); $app->get('/:id/questions', function ($id) use($app) { $ids = explode(';', $id); $questions = Question::whereIn('set_id', $ids)->get(); $questions->load('answers'); $res = $app->response(); $res['Content-Type'] = 'application/json'; $res->body($questions); }); }); $app->group('/question(s)', function () use($app) { $app->get('/', function () use($app) { $questions = Question::with('answers')->get(); $res = $app->response(); $res['Content-Type'] = 'application/json'; $res->body($questions); }); $app->get('/:id', function ($id) use($app) { $question = Question::with('answers')->find($id); $res = $app->response(); $res['Content-Type'] = 'application/json'; $res->body($question); }); $app->get('/:id/answers', function ($id) use($app) { $answers = Question::find($id)->answers; $res = $app->response(); $res['Content-Type'] = 'application/json'; $res->body($answers); }); }); $app->run();
/** * Details page **/ public function getDetails($id, $title) { //First, let's try to find the question: $question = Question::with('users', 'tags', 'answers')->find($id); if ($question) { //We should increase the "viewed" amount $question->update(array('viewed' => $question->viewed + 1)); return View::make('qa.details')->with('title', $question->title)->with('question', $question); } else { return Redirect::route('index')->with('error', 'Question not found'); } }