Laravel php编辑内部数组和数组中的项目

时间:2018-02-12 10:42:39

标签: php laravel loops

我想一次编辑一个有问题和答案的表单。所以这是我的laravel刀片代码

    <form action="{{action('QuizController@postEditQuiz', [$quiz_id])}}" method="post">
    @foreach ($questions  as $key => $question) 

    <input type="text" name="question[]" value="{{$question->question}}">   <br>                
    <input type="hidden" name="question_id[]" value="{{$question->question_id}}">   

    <input type="text" name="correct_answer" value="{{$correct_answer->answer}}"><br>
    <input type="hidden" name="correct_answer_id" value="{{$correct_answer->id}}"><br>


  @foreach($incorrect_answers as $key => $incorrect)
            <input type="text" name="incorrect_answer[]" value="{{$incorrect->answer}}"><br>
            <input type="hidden" name="incorrect_answer_id[]" value="{{$incorrect->id}}"><br>
@endforeach

<input type="submit" name="submit" value="Submit">
</form>

在php方面,我可以毫无问题地更新问题

$question                     = Request::get('question');
$question_id                  = Request::get('question_id');

    $count = count($question);

        for ($i=0;  $i<$count; $i++) {

        $update_question= DB::table('questions')
            ->where('question_id', $question_id[$i])
            ->update([
                'question' => $question[$i],
            ]);
  }

但是,更新每个问题的每个答案都证明是一项挑战。例如,更新上述循环中的corect答案不起作用。

 $question                     = Request::get('question');
 $question_id                  = Request::get('question_id');

    $count = count($question);

        for ($i=0;  $i<$count; $i++) {

        $update_question= DB::table('questions')
            ->where('question_id', $question_id[$i])
            ->update([
                'question' => $question[$i],
            ]);

    $update_correct_answer= DB::table('answers')
            ->where('question_id', $question_id[$i])
            ->where('correct', '1')
            ->update([
                'answer'      => $correct_answer,
            ]);
  }

我应该怎么做,谢谢。

0 个答案:

没有答案