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.
My Answer:
It looks like you are on the right track with your JavaScript and controller code. However, there are a few things that could be causing the issue.
1. Make sure that your route is set up correctly in your web.php file. It should look something like this:
php
Route::post('/subcategories/get_subcategories_by_category', 'YourController@get_subcategories_by_category')->name('subcategories.get_subcategories_by_category');
2. Check if the AJAX request is being sent correctly by adding a console.log statement inside the $.post function:
javascript
$.post('{{ route('subcategories.get_subcategories_by_category') }}',{_token:'{{ csrf_token() }}', category_id:category_id}, function(data){
console.log(data);
// rest of your code
});
3. Make sure that the response from the controller is being returned in the correct format. You can try returning the subcategories as JSON:
php
return response()->json($subcategories);
4. Check if the category_id is being passed correctly to the controller. You can do this by adding a console.log statement in your JavaScript function:
javascript
var category_id = $('#category_id').val();
console.log(category_id);
By following these steps and debugging any potential issues, you should be able to get your search by category and subcategory functionality working correctly.
Rate this post
3 of 5 based on 2979 votesComments