Search by Name Category/SubCategory using Javascript on laravel view


I had some problems with the task. I’m not very good with javascript. Please help.

I display categories as form and I want to when the form to be typed to display products that are set for each category and sub-category. In the next task I have to make sure that when the form is typed, the categories show up and, also without refreshing the page.

code on product_upload.blade.view

                                <div class="form-group row" id="category">
                                <label class="col-md-3 col-from-label">{{translate('Category')}}</label>
                                <div class="col-md-8">
                                    <select class="form-control aiz-selectpicker" name="category_id" id="category_id"  data-live-search="true" required>
                                        @foreach($categories as $category)
                                            <option value="{{$category->id}}">{{ $category->getTranslation('name') }}</option>
                                        @endforeach
                                    </select>
                                </div>
                            </div>
                            <div class="form-group row" id="subcategory">
                                <label class="col-md-3 col-from-label">{{translate('Subcategory')}}</label>
                                <div class="col-md-8">
                                    <select class="form-control aiz-selectpicker" name="subcategory_id" id="subcategory_id"  data-live-search="true" required>

                                    </select>
                                </div>
                            </div>

My script :

       function get_subcategories_by_category(){
        var category_id = $('#category_id').val();
        $.post('{{ route('subcategories.get_subcategories_by_category') }}',{_token:'{{ csrf_token() }}', category_id:category_id}, function(data){
            $('#subcategory_id').html(null);
            for (var i = 0; i < data.length; i++) {
                $('#subcategory_id').append($('<option>', {
                    value: data[i].id,
                    text: data[i].name
                }));
                $(".aiz-selectpicker").selectpicker();
            }
            get_subsubcategories_by_subcategory();
        });
    }

        $('#category_id').on('change', function() {
        get_subcategories_by_category();
    });

    $('#subcategory_id').on('change', function() {
        get_subsubcategories_by_subcategory();
    });

and my controller :

    public function get_subcategories_by_category(Request $request)
{
    $subcategories = SubCategory::where('category_id', $request->category_id)->get();
    return $subcategories;
}

But that wont work.

Source: JavaSript – Stack Overflow

September 3, 2021
Category : News
Tags: controller | javascript | laravel | php

Leave a Reply

Your email address will not be published. Required fields are marked *

Sitemap | Terms | Privacy | Cookies | Advertising

Senior Software Developer

Creator of @LzoMedia I am a backend software developer based in London who likes beautiful code and has an adherence to standards & love's open-source.