dongrong6235 2018-02-16 14:01
浏览 127
已采纳

多个选择字段 - 选择字段必须是一个数组,$ request输出中只显示一个<option>

I have the code below that is a resume of the process to create a new conference. To create a new conference is ncessary that the user introduce some info like the conference name, etc. The user also needs to introduce between 1 and 3 categories for the conference.

So there is a select element using select2 plugin so the user can select the categories:

<div class="form-row">
    <div class="form-group col-lg-6">
        <label for="categories">Category</label>
        <select id="tag_list" required  multiple class="form-control" value="{{ old('categories') }}" name="categories" id="categories">
            @foreach($categories as $category)
                <option value="{{$category->id}}">{{$category->name}}</option>
            @endforeach
        </select>
    </div>
</div>

Then the laravel code to store the conference info and also the categories of the conference in the conference_category table since there is a many to many relationship between confernece and category:

public function store(Request $request)
    {
        $this->validate($request, [
            'conference_name' => 'required|max:255|string',
            'conference_categories' => 'required|array|between:1,3|integer',
        ]);

        $conference = Conference::create([
            'name' => $request->conference_name,
        ]);

        $conference->categories()->attach($request->conference_categories);
    }

The select2 JS:

$(function() {
    $('#tag_list').select2({
        placeholder: '',
        dropdownAutoWidth: 'true',
        width: '100%'
    });
});

Errors

  • If the user selects more than one category in the $request output just appears the id of one category
  • And it appears a laravel validation error after submit the form "The conference categories must be an array. "

Do you know where can be the issue?

  • 写回答

1条回答 默认 最新

  • donglian1384 2018-02-16 14:19
    关注

    You gotta change the name attribute of your select to an array, like this and also remove one id attribute from it, it can't have 2

    <select required  multiple class="form-control" value="{{ old('categories') }}" name="categories[]" id="categories">
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记