I am using Laravel 8 and i want to extract child categories according to parent id's. Below is output image.
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,
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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…