This is create.blade.php file. In this include css and js file too.. Html code and ajax code view file
@extends('layouts.app')
@section('content')
<link rel="stylesheet" href="http://www.codermen.com/css/bootstrap.min.css">
<script src="http://www.codermen.com/js/jquery.js"></script>
<form enctype="multipart/form-data" method="post" action="" >
@csrf
<div class="form-group col-md-8">
Category<select name="category" id="category" class="form-control">
<option>select</option>
@foreach($categories as $category)
<option value=""></option>
@endforeach
</select>
</div>
<div class="form-group col-md-8">
Category<select name="subcategory" id="subcategory" class="form-control">
<option>select</option>
@foreach($subcategories as $subcategory)
<option value=""></option>
@endforeach
</select>
</div>
</form>
<script type="text/javascript">
$('#category').change(function(){
var categoryID = $(this).val();
if(categoryID){
$.ajax({
type:"GET",
url:"?category_id="+categoryID,
success:function(res){
if(res){
$("#subcategory").empty();
$("#subcategory").append('<option>Select</option>');
$.each(res,function(key,value){
$("#subcategory").append('<option value="'+key+'">'+value+'</option>');
});
}else{
$("#subcategory").empty();
}
}
});
}else{
$("#subcategory").empty();
}
});
</script>
@endsection
This is controller code which create function code of category and subcategory
public function create(Request $request){
$categories = Category::all();
$subcategories = DB::table('subcategories')
->where('category_id', $request->category_id)
->pluck('subcategory', 'id');
return view('post.create', compact('categories', 'subcategories'));
}
This is route Route::get('/post/create', 'PostController@create')->name('post.create');
Problem is if i select category still no show related to subcategory
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire