mardi 22 janvier 2019

Not pickup record if already picked up, Laravel 5.7 (Quiz System)

I am making a quiz system in Laravel 5.7 and now i'm fetching questions from the database and I have to apply the condition that if a user attempted a question then not show that question to the user again, for this I have made a database named as attempted_questions and two fields in there as user_id and question_id and applied the following code in controller:

class NewTestController extends Controller
{
    protected $question_id;
    public function nextQuestion(Request $request) {
        $attempted_questions = new AttemptedQuestion;
        $attempted_questions->user_id = Auth::id();
        $attempted_questions->question_id = $request->attempted;
        $attempted_questions->save();
        $question = Question::inRandomOrder()->first();
        $attempted = AttemptedQuestion::all();
        foreach ($attempted as $attempted) {
            while ($question->id == $attempted->question_id) {
                $question = Question::inRandomOrder()->first();
                $this->question_id = $question->id;
            }
        }
        return $this->question_id;
    }
}

Here first I'm getting attempted question from AJAX and then storing the current user and question_id in database and after that I'm getting random questions from database and after that checking if the taken question is already in database? if no then show it otherwise display a message, but the problem here is that it fetches the question also which is already taken



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire