将html表中的表单数据传递给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>

当我检查传递给控制器的数据时,一切都结束了 0 或者 1根据表中的第一个数据。如何获取视图中选定的 rdo 值


慕虎7371278
浏览 150回答 2
2回答

MM们

您只需要为每个输入添加一个 value 属性来表示准确检查的内容。<td>&nbsp; &nbsp; <div class="custom-control custom-radio radio-primary">&nbsp; &nbsp; &nbsp; &nbsp; <input type="radio" id="rdo_{{$key}}_pre" name="rdo[{{$s->id}}]" class="custom-control-input" value="present" checked>&nbsp; &nbsp; &nbsp; &nbsp; <label class="custom-control-label" for="rdo_{{$key}}_pre">Present</label>&nbsp; &nbsp; </div>&nbsp; &nbsp; <div class="custom-control custom-radio radio-pink">&nbsp; &nbsp; &nbsp; &nbsp; <input type="radio" id="rdo_{{$key}}_abs" name="rdo[{{$s->id}}]" class="custom-control-input" value="absent">&nbsp; &nbsp; &nbsp; &nbsp; <label class="custom-control-label" for="rdo_{{$key}}_abs">Absent</label>&nbsp; &nbsp; </div></td>

智慧大石

尝试更改foreach($request->input('chk', []) as $key => $chk)为foreach($request->input('rdo') as $key => $chk)?
打开App,查看更多内容
随时随地看视频慕课网APP