无法以Laravel形式将集合发送到bootstrap-select

时间:2018-02-13 12:31:15

标签: php jquery twitter-bootstrap laravel bootstrap-select

这是我创建的编辑表单

@extends('main')

@section('title', 'Edit Post')

@section('content')

    <form class="form-group" action="/categories/update" method="POST">
        {{csrf_field()}}
        <input name="_method" type="hidden" value="PUT">

        <label for="category_id" id="category_id">Select Category</label>
        <select class="form-control" id="category_id" style="height:auto;" name="category_id">
            @foreach($categories as $category)
                <hr class="custom-hr-heading"/>
                <option value="{{$category->id}}" {{($category->id == $post->category->id)?"selected='selected'":""}}>
                    {{$category->name}}
                </option>
            @endforeach
        </select>

        <label for="title" class="mt-2" name="title" id="title">Your Post Title</label>
        <input type="text" class="form-control mb-2" value="{{$post->title}}" name="title" id="title">

        <textarea class="form-control" name="body">{{$post->body}}</textarea>

        <label for="tags" id="tags" name="tags">Select Tags..</label>

        <select class="selectpicker mt-3" id="tags[]" name="tags[]" multiple="multiple">
            @foreach($tags as$tag)
                <option value="{{$tag->id}}">{{$tag->name}}</option>
            @endforeach
        </select>
        <br/>
        <button type="submit" class="btn mt-3 btn-success btn-sm">Update</button>

    </form>

    <script type="text/javascript">
        tinymce.init({
            selector: 'textarea',
            theme: 'modern',
            height: 300
        });

        $('.selectpicker').selectpicker({
            style: 'btn-info'
        });
    </script>
@stop



Controller
    public function edit($id)
        {
            $post = Post::find($id);
            $categories = Category::orderBy('name', 'asc')->get();
            $tags = Tag::orderBy('name', 'asc')->get();
            return view('posts.edit', compact('post', 'categories', 'tags'))
        }

我得到一个主题选择框,只是没有选择任何内容。我是否正确地将标签集合传递给选择框?

我使用的是Laravel 5.5,PHP 7.2和Bootstrap 4。

我已尝试通过CDN和本地文件进行bootstrap-select。

3 个答案:

答案 0 :(得分:0)

您错过了space

更改:

    @foreach($tags as$tag)

    @foreach($tags as $tag)

答案 1 :(得分:0)

根据Bootstrap 4's documentation,这是一个短属性,而不是名称 - 值对,所以我会这样做(并清理一下):

<select class="form-control" id="category_id" style="height:auto;" name="category_id">
        <option value="">Choose an Option</option>
        @foreach($categories as $category)
            <hr class="custom-hr-heading"/>
            <option value="{{ $category->id }}" @if($category->id == $post->category->id) selected @endif >
                {{ $category->name }}
            </option>
        @endforeach
</select>

答案 2 :(得分:0)

问题实际上是两个问题。 Bootstrap 4(最终)支持仅适用于最新的bootstrap-select beta版本here

我的代码还有一点。这是最终的工作代码。

<select class="selectpicker mt-3 form-control" name="tags[]" multiple>
    @foreach($tags as $tag)
         <option value="{{$tag->id}}" {{$post->tags->contains($tag->id)?"selected='selected":""}}>{{$tag->name}}</option>
    @endforeach
 </select>