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

我有一个带有多选下拉菜单的表单


<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]东西


慕斯王
浏览 217回答 1
1回答

慕村225694

您必须在选择输入名称后添加一对空括号。<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">&nbsp; &nbsp; @foreach(App\DefectType::select('id','title')->get() as $defect_type)&nbsp; &nbsp; &nbsp; &nbsp; <option value="{{$defect_type->id}}">{{$defect_type->title}}</option>&nbsp; &nbsp; @endforeach</select>请注意,name="defect-type-id[]"而只是name="defect-type-id"
打开App,查看更多内容
随时随地看视频慕课网APP