如果复选框位于foreach循环中,如何存储复选框值(laravel)

如果复选框位于foreach循环中,如何存储复选框值(laravel)

问题描述:

I have List of the question and All question is print in a foreach loop. I want to store some question id in the database using checkbox.

@foreach ($allquestion as $indexKey => $all )
          <tr>
            <th>{{$indexKey+1}}</th>
            <th>{{substr($all->question,0,20)}} {{ strlen($all->question)>20 
             ? "..." : ""}}</th>
            <td>{{$all->option1}}</td>
            <td>{{$all->option2}}</td>
            <td>{{$all->option3}}</td>
            <td>{{$all->option4}}</td>
            <td>{{$all->answer}}</td>




            <td>
               <div class="form-check">
                 <input class="form-check-input big-checkbox" 
            name="present[]" type="checkbox" value="{{$all->id}}"   
          id="defaultCheck1">
              </div>
           </td>

          </tr>  @endforeach

I have no idea where I put submit button and how to get value from a checkbox into Controller to save data.

updated

{!! Form::open(array('route' => 'addq', 'data-parsley-validate' => '', 
'files' => true,'method'=>'post')) !!}
 @foreach ($allquestion as $indexKey => $all )
          <tr>
            <th>{{$indexKey+1}}</th>
            <th>{{substr($all->question,0,20)}} {{ strlen($all->question)>20 ? "..." : ""}}</th>
            <td>{{$all->option1}}</td>
            <td>{{$all->option2}}</td>
            <td>{{$all->option3}}</td>
            <td>{{$all->option4}}</td>
            <td>{{$all->answer}}</td>




            <td>
               <div class="form-check">
           <input type="checkbox" class="form-check-input big-checkbox"  
 name="checked[]" value="{{ $all->id }}">
              </div>
           </td>
          </tr>

      @endforeach
    {{Form::submit('Create Test',['class'=>'btn btn-primary'])}}
    {!! Form::close() !!}

When I Put Button like this no action perform.

The same functionality I implemented for one my project and it's working fine,please check the below code. I think it will be useful for you.

HTML:

<input type="checkbox" name="checked[]" value="{{ $employee->id }}">

PHP:

public function store(Request $request)
 {
   foreach ($request->employee_id as $key => $val) 
    {
     $payrolls = new Payroll;
      if (in_array($val, $request->checked))
       {
         $payrolls->basic = $request->basic[$key];
         $payrolls->employee_id = $val;
         $payrolls->save();
       }
    }
return redirect('/');
 }

You Should Try this

Blade File

<form action="Your route">
@foreach ($allquestion as $indexKey => $all )
         <tr>
           <th>{{$indexKey+1}}</th>
           <th>{{substr($all->question,0,20)}} {{ strlen($all->question)>20
            ? "..." : ""}}</th>
           <td>{{$all->option1}}</td>
           <td>{{$all->option2}}</td>
           <td>{{$all->option3}}</td>
           <td>{{$all->option4}}</td>
           <td>{{$all->answer}}</td>

           <td>
              <div class="form-check">
                <input class="form-check-input big-checkbox" name="present[]" type="checkbox" value="{{$all->id}}" id="defaultCheck1">
             </div>
          </td>

         </tr>  
@endforeach
<button type="submit" class="btn btn-primary save">Submit</button>
</form>

In controller:

$present = $request->get('present');
dd($present);