1 month ago

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

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

My edit.blade.php

<form id="category_edit_form" action="{{route('categories.update', $category->id)}}" method="POST"
                    <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>
                                        @include('admin.category.subcategoryList',['subcategories' => $parentCategory->subcategory])
                                <p class="error alert alert-danger">{{$errors->first('parent_id')}}</p>
                        <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">
                                <p class="error alert alert-danger">{{$errors->first('name')}}</p>
                        <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">
                                <p class="error alert alert-danger">{{$errors->first('slug')}}</p>

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

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.