Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
367 views
in Technique[技术] by (71.8m points)

controller - How to control data repeatation in Laravel?

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

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

1 Reply

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...