将html表中的表单数据传递给laravel中的控制器

时间:2019-06-28 16:26:47

标签: php laravel

我是laravel的新手,我想将数组数据从html表传递到控制器

我尝试使用此代码

控制器

foreach($request->input('chk', []) as $key => $chk){
   $att = new Attendance;
   $att->student_id = $key;
   $att->date = $request->att_day;
   $status = $chk == 'true' ? 1 : 0;
   $att->status = $status;

}

查看

<form action="{{route('attendance.save')}}" method="post" class="form-horizontal form-bordered">
<table class="table">
   <thead>
      <tr>
         <th class="text-center">#</th>
         <th>Student Name</th>
         <th>Status</th>
      </tr>
   </thead>
   <tbody>
   @foreach ($students as $key => $s)
      <tr>
         <td class="text-center">{{ $key + 1 }}</td>
         <td>{{ $s->student_name }}</td>
         <td>
            <div class="custom-control custom-radio radio-primary">
               <input type="radio" id="rdo_{{$key}}_pre" name="rdo[{{$s->id}}]" class="custom-control-input" checked>
               <label class="custom-control-label" for="rdo_{{$key}}_pre">Present</label>
            </div>
            <div class="custom-control custom-radio radio-pink">
               <input type="radio" id="rdo_{{$key}}_abs" name="rdo[{{$s->id}}]" class="custom-control-input">
               <label class="custom-control-label" for="rdo_{{$key}}_abs">Absent</label>
            </div>
         </td>
      </tr>
   @endforeach
   </tbody>
</table>
</form>

当我检查传递到控制器的数据时,根据表中的第一个数据,它们都是。如何获取视图中选定的rdo值

2 个答案:

答案 0 :(得分:0)

尝试将foreach($request->input('chk', []) as $key => $chk)更改为foreach($request->input('rdo') as $key => $chk)吗?

答案 1 :(得分:0)

您只需要向每个输入添加一个value属性,以表示已正确检查的内容。

<td>
    <div class="custom-control custom-radio radio-primary">
        <input type="radio" id="rdo_{{$key}}_pre" name="rdo[{{$s->id}}]" class="custom-control-input" value="present" checked>
        <label class="custom-control-label" for="rdo_{{$key}}_pre">Present</label>
    </div>
    <div class="custom-control custom-radio radio-pink">
        <input type="radio" id="rdo_{{$key}}_abs" name="rdo[{{$s->id}}]" class="custom-control-input" value="absent">
        <label class="custom-control-label" for="rdo_{{$key}}_abs">Absent</label>
    </div>
</td>