Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a quiz. The quiz has 10 questions. First time I saved 7 questions of the quiz using the below code.In the second case I wanted to save the quiz questions again. But in this case the data is being duplicated. I want the question that have been saved once not to be saved in the second time.

     foreach($request->input('questions', []) as $key => $question){
            QuizSessionAnswer::create([
                'session_id'=> $sessionId,
                'question_id'=> $question,
                'selected_choice_id'=> $request->input('choice.'.$question),
                'created_by_id'=> auth()->user()->id,
            ]);
        }

How can I solve the problem?

question from:https://stackoverflow.com/questions/65850602/how-to-control-data-repeatation-in-laravel

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
387 views
Welcome To Ask or Share your Answers For Others

1 Answer

You can one of two

  1. try updateOrCreate method, you can review Laravel Reference here.
  2. you need to identify in your "view form" if you will create a QUIZ questions (send to a quiz.create route) or edit your questions (send to a quiz.edit route)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...