在Laravel中使用嵌套类别创建选择选项按钮

时间:2018-09-26 15:48:56

标签: php laravel select categories option

不好意思,请问我:) 我有一个包含多级类别的表:

category_id ------ category_name--------parent_id
1------------------sample 1 ------------ NULL
2------------------sample 2 ------------ 1
3------------------sample 3 ------------ NULL

和...

现在我需要在选择选项菜单中保存此数据

这是我的表格:

<div class="form-group">
        <label class="col-sm-2 control-label">مادر</label>
        <div class="text-right col-sm-10">
            <select name="parent_id" class="select2 form-control" id="parent_id">
                <option value="">انتخاب دسته مادر</option>
                @foreach ($categories as $cat)
                    <option value="{{ $cat -> category_id }}"
                            {{ isset($productItem) && $productItem->category_id == $cat->category_id ? 'selected':'' }}
                    >{{ $cat -> category_name }}</option>
                @endforeach
            </select>
        </div>
    </div>

3 个答案:

答案 0 :(得分:1)

您应该使用<optgroup>进行一些“嵌套”的选择。选中this example

您的代码应为:

<select name="parent_id" class="select2 form-control" id="parent_id">
  <option value="">انتخاب دسته مادر</option> <!-- default -->
  @foreach ($categories->where('parent_id', null) as $cat) <!-- first level only: no parent -->

    <!-- if parent must also be selectable, you can add an option for it before its optgroup -->
    <option value="{{ $cat->category_id }}">
      {{ $cat->category_name }}
    </option>

    @if($cat->children->count()) <!-- if has children -->
    <optgroup label="{{$cat->category_name}}"> <!-- display parent optgroup and within child categories option -->
      @foreach($cat->children as $child)
        <option value="{{ $child -> category_id }}">
          {{ $child -> category_name }}
        </option>
      @endforeach
    </optgroup>
    @endif
  @endforeach
</select>

这应该创建:

    每个父类别的
  • <optgroup>
  • <option>放在父类别的optgroup之前
  • 每个子类别的
  • <option>

答案 1 :(得分:1)

希望找到一个很好地使用Laravel的答案后,我最终放弃了,只写了代码来做自己想做的事,结果比我预期的要小。

public function get_categories()
{

    $categories = Category::where('parent',null)->get();
    while(count($categories) > 0){
        $nextCategories = [];
        echo "<option value='.$categories->.'>'.$categories->name .'</option";
        foreach ($categories as $category) {
          $childCategories = Category::where('parent',$category->id)->get();
          echo "<option value='.$categories->.'>'.$childCategories->name .'</option";
        }
    }

}

答案 2 :(得分:0)

should be like this

but in level 3 I have the problem

然后我更改了代码:

<select name="parent_id" class="select2 form-control" id="parent_id">
                <option value="">انتخاب دسته مادر</option>
                @foreach ($categories as $cat)
                    @if($cat -> parent_id == NULL)
                        <option value="{{ $cat -> category_id }}">
                            {{ $cat -> category_name }}
                        </option>
                    @endif
                    @if ($cat->children->count() > 0)
                        @foreach($cat->children as $child)
                            <option value="{{ $child -> category_id }}">
                                {{ $child -> category_name }}
                            </option>
                        @endforeach
                    @endif
                @endforeach
            </select>