为了账号安全,请及时绑定邮箱和手机立即绑定

Laravel 获取多选下拉值到控制器

Laravel 获取多选下拉值到控制器

PHP
慕斯王 2022-01-02 15:55:57
我有一个带有多选下拉菜单的表单<form role="form" method="post" action="{{route('dev-admin.developer-contractor-associations.add.post')}}" autocomplete="off">        <div class="col-12">            @csrf            <div class="form-group {{ $errors->has('defect-type-id') ? ' has-danger' : '' }}">                <select class="selectpicker {{ $errors->has('defect-type-id') ? 'is-invalid' : '' }}" name="defect-type-id" id="defect-type-id" multiple data-style="selectpicker-style" data-width="100%" title="Defect Types">                    @foreach(App\DefectType::select('id','title')->get() as $defect_type)                        <option value="{{$defect_type->id}}">{{$defect_type->title}}</option>                    @endforeach                </select>                @if ($errors->has('defect-type-id'))                <span class="invalid-feedback" role="alert">                    <strong>{{ $errors->first('defect-type-id') }}</strong>                </span>                @endif            </div>        </div>        <div class="col-12">        <div class="text-center">            <button onclick="return confirm('Are you sure to associate this contractor?')" type="submit" class="btn btn-primary my-4">Associate</button>        </div>    </div></form>当我想获取控制器中的值时,我只收到 1 个选定值而不是选定选项列表:public function postAddDeveloperContractorAssociation(Request $request ) {    $defect_type_id = $request->input('defect-type-id');    dd($defect_type_id);    return redirect()->route('dev-admin.developer-contractor-associations.index')->withStatus(__('Contractor has been added.'));}当我dd这一切时,我得到的是类似的东西"2"而不是类似的[1, 2, 3]东西
查看完整描述

1 回答

?
慕村225694

TA贡献1880条经验 获得超4个赞

您必须在选择输入名称后添加一对空括号。


<select class="selectpicker {{ $errors->has('defect-type-id') ? 'is-invalid' : '' }}" name="defect-type-id[]" id="defect-type-id" multiple data-style="selectpicker-style" data-width="100%" title="Defect Types">

    @foreach(App\DefectType::select('id','title')->get() as $defect_type)

        <option value="{{$defect_type->id}}">{{$defect_type->title}}</option>

    @endforeach

</select>

请注意,name="defect-type-id[]"而只是name="defect-type-id"


查看完整回答
反对 回复 2022-01-02
  • 1 回答
  • 0 关注
  • 216 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信