首页 > 解决方案 > 如何在控制器 laravel 5 中为每个接收递增的 id

问题描述

我有一个表单,我现在通过未知大小的 foreach 增加复选框的 ID,当我提交时,我想接收所有发送的数据,这是我的代码:

 <form action="/somecontroller" method="post">
                    <div  id="checkboxes" class="col-lg-2 text-center">
                        <input type="checkbox" name="rGroup" name="d{{$index}}" value="{{verta($pdates->date)->format('Y/m/d')}}" id="d{{$index}}"/>
                        <label class="whatever mt-3" for="d{{$index}}"> {{verta($pdates->date)->format('Y/m/d')}}
                            <hr>
                            {{$pdates->price}}</label>
                    </div>
                    </form>

这是控制器:

 $recived_data = $request->d{{$index}}; here i want to get all the checkboxes send by user

那么我如何接收用户发送的我不知道复选框数量的数据

标签: phplaravel

解决方案


您可以使用输入元素的数组语法,并在控制器中接收带有所选项目的数组,因此将输入复选框更改为:

<input type="checkbox" name="d[{{$index}}]" value="{{verta($pdates->date)->format('Y/m/d')}}" id="d{{$index}}"/>

然后在你的控制器中:

$request->input('d'); // returns an array of indexes of all the selected checkboxes.

推荐阅读