• 24-12-2022, 18:30:27
    #1
    Merhabalar,
    Laravelde tüm root ve alt kategorileri getirebiliyorum ancak tek sıkıntım html <option> ile kullanırken sorun yaşıyorum. Kategorilerden hangisi root hangisi subcategory belirtemiyorum. Kategorilerin hepsini çekiyor.
    category_add.blade.php
    <select class="form-control select2" style="width: 100%;" name="category">
        <option value="">Kategori Seçiniz</option>
        @foreach ($categories as $category)
            <option value="{{ $category->id }}">{{ $category->name }}</option>
            @if ($category->categories)
                <ul>
                    @foreach ($category->categories as $childCategory)
                        @include('admin.child_category', ['child_category' => $childCategory])
                    @endforeach
                </ul>
            @endif
        @endforeach
    </select>
    child_category.blade.php
    <option value="{{ $childCategory->id }}">{{ $childCategory->name }}</option>
    @if ($child_category->categories)
    <ul>
    @foreach ($child_category->categories as $childCategory)
    @include('admin.child_category', ['child_category' => $childCategory])
    @endforeach
    </ul>
    @endif
  • 24-12-2022, 19:24:44
    #2
    Parent kategorilere optgroup tanimladiginizda sorun hallolur gibi.
    Mobildeyim kodlarini duzenleyemedim. Asagidaki kodu kendine gore duzenleyebilirsin.

    <select name="" id="">
    @forelse ($categories as $category)
    <optgroup label="{{$category->name}}">
    @forelse($category->child as $child)
    <option value="{{$child->id}}">{{$child->name}}</option>
    @empty
    <option value="">No category</option>
    @endforelse
    </optgroup>
    @empty @endforelse
    </select>