Support the ongoing development of Laravel.io →
Laravel Database Eloquent
Last updated 2 years ago.
0

First of all, some "off-topic" stuff:

return view('questions', ['questions' => $questions]);

This can be turned into:

return view('questions', compact('questions'));

You'll still get the same result, but it's something I personally recommend. Same for the getAnswers() function in the QuestionController.

--

Why are you making a POST request to getAnswers? Unless you're actually posting something, I'd say change it to a GET request instead.

--

The questions you're asking are quite difficult to answer directly due to not knowing the full setup of your app. However I'll let you know how I would have done things if I was you - if it's not something that would work, simply reply and we'll take it from there :)

We'd need the following tables:

  1. Questions (id, question_text)
  • id = autoincrement
  • question_text = the actual question
  1. Question_Answers (id, question_id, answer_text, answer)
  • id = autoincrement
  • question_id = the questions.id
  • answer_text = the actual answer
  • answer = boolean (true/false) - is this the correct answer to the question?
  1. User_Answers(id, question_id, answer_id, user_id)
  • id = autoincrement
  • question_id = the questions.id
  • answer_id = the question_answers.id (selected by the user)
  • user_id = the logged in user ID

--

  • How can I save the answer from the logged in User?

First, check if the user is logged in:

if (Auth::check()) {
    // The user is logged in...
}

If the user is logged in, send a POST request to the "User_Answers" table store the user's answer, which includes the Question ID, the ID of the selected answer, and the user ID. You get the user id like this: $id = Auth::id();

--

  • check if the user answered the question correctly?

Since we've stored the correct answer in the Question_Answers table, using User_Answers.answer_id we can query Question_Answers and find the id that matches, and check the answer boolean. If it is true, the user has selected the correct answer. If it's false, the user's answer was incorrect.

--

  • How can I check if the user already answered the question, and if yes, how can I lock it?

Simply query the User_Answers table and check if the user's ID and question_id can be found. If yes, the user has already answered the question. If no, the user hasn't.

By "Locking it", do you mean not letting anyone answer the same question, or simply lock it for the user in question? If you want to lock it for everyone, you need to add a "Locked" column to the "Questions" table where a boolean true/false would tell us if it's locked or not.

If you want to lock it specifically for the user only, I assume you'd have to make a query like in the previous question, checking if the question is already answered, and if yes simply skip it on the front-end?

--

  • ** I want to give the user points for a correct answer. How can I store and save this ? (I think in another table with the user_id?)**

Correct! And incorrect! Depends on how you want to do it, really :) You can choose to add a "points" column to the already existing "Users" table provided by Laravel for authentication. Or you can create a new table like you said, called "User_Points", where you would store the user_id and it's points.

Personally I would simply add the column to the "Users" table to keep it simple. A small warning though; if you accidentally remove the built-in authenticator, it will remove the users table and all users' points would be lost. But as long as you're aware of this, I'm sure you wouldn't remove the auth system :)

I'm sure I missed lots of points, but for now I think I covered the basics.

0

Sign in to participate in this thread!

Eventy

Your banner here too?

Alex Alex rogo Joined 5 Jul 2017

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.