【问题标题】:Laravel get multi selected drop down value to controllerLaravel 获取多选下拉值到控制器
【发布时间】:2019-09-06 03:11:40
【问题描述】:

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

<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] 或类似的东西

【问题讨论】:

  • 将其命名为数组 name="defect-type-id[]"
  • 嗨@porloscerrosΨ,您的意思是在我的刀片或控制器上的输入文件中
  • 嗨,在刀片中,选择 &lt;select name="defect-type-id[]" multiple&gt; 的名称。看看这个帖子stackoverflow.com/a/14431457/7498116
  • 谢谢你的工作,你能把你的答案写在下面,所以我接受它作为正确答案
  • 没关系,再次感谢

标签: php laravel laravel-5 eloquent


【解决方案1】:

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

<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"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-31
    • 1970-01-01
    • 2015-05-22
    • 2021-09-11
    • 1970-01-01
    • 1970-01-01
    • 2012-02-02
    相关资源
    最近更新 更多