public function getSectionsForClass(Request $request ,$id )
{
$section = Section::all()->where('clas_id',$id);
return response()->json($section);
}
I need to send this json to my view in table like
@foreach($section as $sec)
{{$sec->section}}
{{$sec->capacity}}
{{$sec->teacher}}
{{$sec->class}}
@endforeach
This is my ajax code where i have send id and url
<script type="text/javascript">
$('#select_id').change(function(){
// alert('hello');
var cid = $(this).val();
if(cid){
$.ajax({
dataType: "json",
url: 'section/index/'+cid,
//data: {'class': cid},
type:"GET",
success: function(response){
console.log ((response));
},error: (error) => {
console.log(JSON.stringify(error));
}
});
}
});
</script>
In Route
Route::get('admin/section/index/{id}','SectionController@getSectionsForClass');
Thanks in advance hope I will get my answer
2
Answers
As per the Laravel Documentation you can directly send the
json
response from your methodAnd then you can create dynamic table from your ajax success at append that table in specific id for e.g
Laravel -> Http Responses -> Json Responses
Currently, you are returning an instance of the Query Builder as a json response. You haven’t execute the query yet.
To get the results from your query you need to actually execute the query. Add
get()
at the end:Of course, this could be better managed in a paginated list in case you have lots of records, but you get the idea.