2

excuse me for my bad language :) I have a table with multilevel categories like this :

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

and...

now I need to have this data in the select option menu

this is my form :

<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>
aynber
  • 22,380
  • 8
  • 50
  • 63

3 Answers3

1

You should use <optgroup> to make some "nested" options. Check this example.

Your code would be:

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

This should create:

  • <optgroup> for each parent categories
  • <option> placed just before optgroup for the parent categories
  • <option> for every child categories
Mtxz
  • 3,749
  • 15
  • 29
0

should be like this

but in level 3 I have the problem

and I changed my code :

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

After hoping to find an answer that uses Laravel nicely, I ended up giving up and just writing the code to do what I wanted myself, and it ended up smaller than I anticipated.

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

}

Rahman Rezaee
  • 1,943
  • 16
  • 24