l00p
1 month ago
265
6
Laravel

How to exclude child and subcategories while editing in multi level category in laravel?

Posted 1 month ago by l00p

I have created multi level category in laravel project. And I am using recursive function to load the subcategories of category. Let my category be https://imgur.com/M6DGj5A and my form is https://imgur.com/PAy4Nk9

Now my question is if I edit any category then I don't want its child and subcategories to be in select dropdown. Suppose if I edit Laravel then its children and itself(Laravel) should not appear in select dropdown. And if I edit Laravel 1 then I don't want Laravel 1.1 to be in select dropdown. In general, the parent category cannot be the subcategory of its child and itself. How can I achieve this?

My subcategoryList.blade.php

<optgroup>
            @if($subcategory->id != request()->route('category'))
                <option value="{{$subcategory->id}}" {{$subcategory->id == $category->parent_id ? 'selected' : ''}}>{{$subcategory->name}}</option>
            @endif
        </optgroup>

My edit.blade.php

<form id="category_edit_form" action="{{route('categories.update', $category->id)}}" method="POST"
                      enctype="multipart/form-data">
                    @csrf
                    @method('PUT')
                    <input type="hidden" value="{{$category->id}}" class="category_id" name="category_id">
                    <div class="form-row">
                        <div class="form-group col-md-12">
                            <label for="">Select Category</label>
                            <select name="parent_id" class="form-control" id="">
                                <option value="">Select Category</option>
                                @foreach($parentCategories as $parentCategory)
                                    <option value="{{$parentCategory->id}}" {{$parentCategory->id == $category->parent_id ? 'selected' : ''}}>{{$parentCategory->nepali_name}}</option>
                                    @if(count($parentCategory->subcategory))
                                        @include('admin.category.subcategoryList',['subcategories' => $parentCategory->subcategory])
                                    @endif
                                @endforeach
                            </select>
                            @if($errors->has('parent_id'))
                                <p class="error alert alert-danger">{{$errors->first('parent_id')}}</p>
                            @endif
                        </div>
                        
                        <div class="form-group col-md-12">
                            <label for="">Name:</label>
                            <input type="text" name="name" id="name" value="{{$category->name}}"
                                   class="form-control name">
                            @if($errors->has('name'))
                                <p class="error alert alert-danger">{{$errors->first('name')}}</p>
                            @endif
                        </div>
                        <div class="form-group col-md-12">
                            <label for="">Slug:</label>
                            <input type="text" name="slug" id="slug" value="{{$category->slug}}"
                                   class="form-control slug">
                            @if($errors->has('slug'))
                                <p class="error alert alert-danger">{{$errors->first('slug')}}</p>
                            @endif
                        </div>
                    </div>

                    <div class="form-group text-center">
                        <button type="submit" class="btn btn-primary" id="editCategoryData">Submit</button>
                    </div>
                </form>

My controller:

public function edit($id)
    {
        $category = Category::find($id);
        $parentCategories = Category::where('parent_id',0)->where('id','<>',\request()->route('category'))->get();
        return view('admin.category.edit', compact('category', 'parentCategories'));
    }

Please sign in or create an account to participate in this conversation.