Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
545 views
in Technique[技术] by (71.8m points)

laravel - How to display Parent and child categories in lravel view?

I am using Laravel 8 and i want to extract child categories according to parent id's. Below is output image. enter image description here

Each item contains parent filed that represent its parent id. Now i want that in dropdown i display child categories below parent. I have following code,

 <div class="form-group col-md-4">
            <label for="categories">Product Category</label>
            <select class="select2 form-control select2-multiple" multiple="multiple"
                    data-placeholder="Select Product Category" name="categories[]">

                @forelse($categories as $keyParent => $category)
                    <optgroup label="{{$category->name}}">
                        <option value="{{$category->id}}">{{$category->name}}</option>
                    @foreach($categories as $keyChild => $sub)
                            @if($sub->parent === $category->id)
                                @unset($categories[$keyChild])
                                <option value="{{$sub->id}}">{{$sub->name}}</option>
                            @endif
                        @endforeach
                    </optgroup>
                @empty
                @endforelse
            </select>
        </div>

But i get child again. Below is out put,

enter image description here

Hoodies contain there child below but also repeat Man. How can i solve this. Thanks

question from:https://stackoverflow.com/questions/65933374/how-to-display-parent-and-child-categories-in-lravel-view

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...