Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

anilkumarthakur60's avatar

get list of Subcategory while clicking in the Category

how can i get the subcategory list while clicking in the category list

here is my route

Route::resource('admindashboard/subcategory','Admin\SubCategory\SubCategoryController');
Route::resource('admindashboard/category','Admin\Category\CategoryController');
Route::resource('admindashboard/products','Admin\Products\ProductController');

here is my blade file

 <div class="col-lg-4">
                      <div class="form-group mg-b-10-force">
                        <label class="form-control-label">Category: <span class="tx-danger">*</span></label>
                        <select class="form-control select2" name="category_id" data-placeholder="Choose Category">
                            <option label="Choose Category">Choose Category</option>
                            @foreach ($categories as $category)
                            <option value="{{ $category->id }}">{{$category->name  }}</option>
                            @endforeach

                        </select>
                      </div>
                    </div><!-- col-4 -->


                    <div class="col-lg-4">
                        <div class="form-group mg-b-10-force">
                          <label class="form-control-label">Sub Category: <span class="tx-danger">*</span></label>
                          <select class="form-control select2" name="subcategory_id" data-placeholder="Choose Sub Category">
			//get list of sub categories while selecting category
                          </select>
                        </div>
                    </div><!-- col-4 -->

here is my product controller

 public function index()
    {
        return view('admin.products.index');
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.products.add')->with('categories',Category::all())->with('brands',Brand::all());
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }

field name in categories and subcategories are

 public function up()
    {
        Schema::create('categories', function (Blueprint $table) {
            $table->id();
            $table->string('name')->unique();
            $table->timestamps();
        });
    }

 public function up()
    {
        Schema::create('sub_categories', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->integer('category_id');
            $table->timestamps();
        });
    }

i think this can be done through ajax but i dont know help me with code

0 likes
2 replies
anilkumarthakur60's avatar
Level 6

using ajax

<script type="text/javascript">
    $(document).ready(function(){
   $('select[name="category_id"]').on('change',function(){
        var category_id = $(this).val();
        if (category_id) {

          $.ajax({
            url: "{{ url('/get/subcategory/') }}/"+category_id,
            type:"GET",
            dataType:"json",
            success:function(data) {
            var d =$('select[name="subcategory_id"]').empty();
            $.each(data, function(key, value){

            $('select[name="subcategory_id"]').append('<option value="'+ value.id +'">' + value.name + '</option>');

            });
            },
          });

        }else{
          alert('danger');
        }

          });
    });

</script>

route

Route::get('get/subcategory/{category_id}','Admin\Products\ProductController@GetSubcate');

controller is

 public function GetSubcate($category_id){
      
        $cat=SubCategory::where('category_id',$category_id)->get();
        return json_encode($cat);
        //using route model binding

    }

Please or to participate in this conversation.