Skip to content
Advertisement

How to save an array of input in Laravel

I am new to Laravel, Can someone tell me how can i save array of input using my controller.

My controller

$answers = $request->answer;

foreach ($answers as $answer) 
{
    $ans = new Answer;
    $ans->question_id = $question->id;
    $ans->answer = $answer['body'];  
    $ans->is_correct = (array_key_exists('check', $answer) && $answer['check'] == 'on')
        ? true
        :false;

    $ans->save();
} 

My view

<table class=".table-borderless" align="center" id="tbl-insert-answers">
  <tbody>
    <tr>
      <td>
        <input required type="text" name="answer[body][]" >
      </td>
      <td>
        <input type="checkbox" name="answer[check][]" >
      </td> 
    </tr> 
    <tr>
      <td>
        <input required type="text" name="answer[body][]" >
      </td>
      <td>
        <input type="checkbox" name="answer[check][]" >
      </td> 
    </tr> 
  </tbody>
</table>

it give me error: Undefined index: body

Advertisement

Answer

This could be done this way

View

 <table class="table table-hover" align="left" id="tbl-insert-answers">

                                    <thead>
                                        <tr>
                                            <td style="text-align:left;">Answers</td>
                                            <td>Correct</td>
                                            <td>Delete</td>
                                        </tr>
                                    </thead>
                                    <tbody>
                                    <!-- to remove the error for non-object -->
                                        @if(@$question->answer)         
                                        <?php $i=0;?>
                                            @foreach($question->answer as $answer) 
                                                <tr>
                                                    <td><input required type="text" name="answer[{{$i}}][body]" value="{{old('answer[body]',$answer->answer)}}"></td>
                                                    <td><input class="ans_chbox" type="checkbox"  name="answer[{{$i}}][check]" value="on" {{($answer->is_correct)?'checked':''}}></td>
                                                    <td><a href="javascript:void(0)" class="fa fa-close btn-sm remove" style="font-size:20px;color:red"></a></td>
                                                <?php $i++; ?>  
                                                </tr>   
                                            @endforeach
                                        @else

                                            <tr>
                                                <td><input required type="text" placeholder="Enter Answer" name="answer[0][body]"></td>
                                                <td><input type="checkbox" name="answer[0][check]"></td>
                                                <td style="visibility:hidden;"><a href="javascript:void(0)" class="fa fa-close btn-sm remove" style="font-size:20px;color:red"></a></td>
                                            </tr> 
                                            <!-- <tr>
                                                <td><input required type="text" placeholder="Enter Answer" name="answer[1][body]"></td>
                                                <td><input type="checkbox" name="answer[1][check]"></td>
                                                <td style="visibility:hidden;"><a href="javascript:void(0)" class="fa fa-close btn-sm remove" style="font-size:20px;color:red"></a></td>
                                            </tr> -->
                                        @endif         
                                    </tbody>
                                    </table>


And we can update the controller like this

Controller

 $answers=$request->answer;
        foreach($answers as $answer){
        Answer::create([ "answer" => $answer['body'],
        "is_correct"=>(array_key_exists('check',$answer) && $answer['check'] == 'on')?true:false,]
        +['question_id' => $question->id]);   
        }
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement